Monday , January 10 2022

Tic Tac Toe Android App Tutorial with MiniMax Algorithm – Android Coding by DMTechnolab

class group {{

// Strings for stringers and computers

partner thing {{

Const Val The player = = “The”

Const Val Computer = = “X”

}

// this is our inner board

// And for this we used 3 by 3 arrays of strings

Val group = = Array()3) {{ arrayOfNulls<String>()3) }

// giving us this property

// a list of all empty cells

Val available: The list<Cell>

get()) {{

Val Cells = = mutableListOf<Cell>())

For ()I am Is in group.Indicates) {{

For ()J Is in group.Indicates) {{

If ()group[i][j].isNullOrEmpty())) {{

Cells.Add up()Cell()I am, J))

}

}

}

The return Cells

}

// it will tell the property

// if the game is over or not

Val isGameOver: Boolean

get()) = = hasComputerWon()) || hasPlayerWon()) || available.Is Empty())

// These functions are checked

// weather whether computer or player has won

enjoy hasComputerWon()): Boolean {{

If ()group[0][0] == group[1][1] &&

group[0][0] == group[2][2] &&

group[0][0] == Computer ||

group[0][2] == group[1][1] &&

group[0][2] == group[2][0] &&

group[0][2] == Computer

) {{

The return True

}

For ()I am Is in group.Indicates) {{

If ()

group[i][0] == group[i][1] &&

group[i][0] == group[i][2] &&

group[i][0] == Computer ||

group[0][i] == group[1][i] &&

group[0][i] == group[2][i] &&

group[0][i] == Computer

) {{

The return True

}

}

The return false

}

enjoy hasPlayerWon()): Boolean {{

If ()group[0][0] == group[1][1] &&

group[0][0] == group[2][2] &&

group[0][0] == The player ||

group[0][2] == group[1][1] &&

group[0][2] == group[2][0] &&

group[0][2] == The player

) {{

The return True

}

For ()I am Is in group.Indicates) {{

If ()

group[i][0] == group[i][1] &&

group[i][0] == group[i][2] &&

group[i][0] == The player ||

group[0][i] == group[1][i] &&

group[0][i] == group[2][i] &&

group[0][i] == The player

) {{

The return True

}

}

The return false

}

// In this version we will store computers

Where computer: CellThe = = Invalid

// This is our minimum work to calculate

// best move for computer

enjoy the minimum()The depth: Int, The player: String): Int {{

If ()hasComputerWon())) The return +1

If ()hasPlayerWon())) The return 1

If ()available.Is Empty())) The return 0

Where M = = Maximum Marks.Max value

Where Maximum = = Maximum Marks.MIN_VALUE

For ()I am Is in available.Indicates) {{

Val Cell = = available[i]

If ()The player == Computer) {{

place()Cell, Computer)

Val current score = = the minimum()The depth + 1, The player)

Maximum = = The monastery.Maximum()current score, Maximum)

If ()current score > = 0) {{

If ()The depth == 0) computer = = Cell

}

If ()current score == 1) {{

group[cell.i][cell.j] = = “”

break

}

If ()I am == available.Shape 1 && Maximum < 0) {{

If ()The depth == 0) computer = = Cell

}

} other If ()The player == The player) {{

place()Cell, The player)

Val current score = = the minimum()The depth + 1, Computer)

M = = The monastery.M()current score, M)

If ()M == 1) {{

group[cell.i][cell.j] = = “”

break

}

}

group[cell.i][cell.j] = = “”

}

The return If ()The player == Computer) Maximum other M

}

// This function is a move in a given cell

enjoy place()Cell: Cell, The player: String) {{

group[cell.i][cell.j] = = The player

}

}


Source link

About dmtechnolab

Check Also

Simplified Coding

Android Espresso Tutorial – Testing Fragment in Isolation – Android Coding by DMTechnolab

Welcome to another tutorial in the Android test series. And this post is called Android …

Leave a Reply

Your email address will not be published. Required fields are marked *