Monday , January 10 2022
Breaking News

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())) {{

}

}

}

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 == group &&

group == group &&

group == Computer ||

group == group &&

group == group &&

group == Computer

) {{

The return True

}

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

If ()

group[i] == group[i] &&

group[i] == group[i] &&

group[i] == Computer ||

group[i] == group[i] &&

group[i] == group[i] &&

group[i] == Computer

) {{

The return True

}

}

The return false

}

enjoy hasPlayerWon()): Boolean {{

If ()group == group &&

group == group &&

group == The player ||

group == group &&

group == group &&

group == The player

) {{

The return True

}

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

If ()

group[i] == group[i] &&

group[i] == group[i] &&

group[i] == The player ||

group[i] == group[i] &&

group[i] == group[i] &&

group[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

}

} 