-
Notifications
You must be signed in to change notification settings - Fork 0
/
noughtsCrosses.kt
135 lines (125 loc) · 4.37 KB
/
noughtsCrosses.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.lang.NumberFormatException
fun gameLogic(grid: MutableList<MutableList<Char>>): String {
var convertThreeCharsToString = ""
var gridDiagonalColumnIndex = 0
var gameResult = ""
//Rows
for (j in 0..2) {
for (i in 0..2) {
convertThreeCharsToString += grid[j][i]
if (convertThreeCharsToString == "OOO" || convertThreeCharsToString == "XXX") {
gameResult = "${convertThreeCharsToString[0]} wins."
break
}
}
convertThreeCharsToString = ""
}
//Columns
for (j in 0..2) {
for (i in 0..2) {
convertThreeCharsToString += grid[i][j]
if (convertThreeCharsToString == "OOO" || convertThreeCharsToString == "XXX") {
gameResult = "${convertThreeCharsToString[0]} wins."
break
}
}
convertThreeCharsToString = ""
}
// Diagonals
for (i in 0..2){
convertThreeCharsToString += grid[i][i]
if (convertThreeCharsToString == "XXX" || convertThreeCharsToString == "OOO"){
gameResult = "${convertThreeCharsToString[0]} wins."
break
}
}
convertThreeCharsToString = ""
for (i in 2 downTo 0){
convertThreeCharsToString += grid[i][gridDiagonalColumnIndex]
gridDiagonalColumnIndex += 1
if (convertThreeCharsToString == "XXX" || convertThreeCharsToString == "OOO"){
gameResult = "${convertThreeCharsToString[0]} wins."
break
}
}
return gameResult
}
fun printTheGameGrid(grid: MutableList<MutableList<Char>>) {
println("---------")
for (i in 0..2){
print("| ")
for (j in 0..2){
if (grid[i][j] == '_') {
grid[i][j] = ' '
}
print("${grid[i][j]} ")
}
print("|")
println()
}
println("---------")
}
fun userInputToGameGrid (grid: MutableList<MutableList<Char>>, drawGridSymbols: String): MutableList<MutableList<Char>> {
var counter =0
for (i in 0..2) {
for (j in 0..2) {
grid[i][j] = drawGridSymbols[counter]
counter += 1
}
}
return grid
}
fun changeTurn(flag: Int): Char{
var turn = 'X'
if (flag % 2 == 0){
turn ='O'
}
return turn
}
fun main() {
var gameTurnSwitch = 1
var numberOfMoves = 1
var makeAMove: List<Int>
val grid = MutableList(3) { MutableList(3) { '_' } }
val drawGridSymbols = "_________"
val turnUserInputToGameGrid = userInputToGameGrid(grid, drawGridSymbols)
if (gameLogic(turnUserInputToGameGrid) != "") {
print(gameLogic(turnUserInputToGameGrid))
}
printTheGameGrid(turnUserInputToGameGrid)
while (true) {
try {
makeAMove = readln().split(" ").map { it.toInt() }
when {
makeAMove[0] > 3 || makeAMove[0] == 0 -> println("Coordinates should be from 1 to 3!")
makeAMove[1] > 3 || makeAMove[1] == 0 -> println("Coordinates should be from 1 to 3!")
turnUserInputToGameGrid[makeAMove[0] - 1][makeAMove[1] - 1] != ' ' -> println("This cell is occupied! Choose another one!")
numberOfMoves == 9 -> {
turnUserInputToGameGrid[makeAMove[0] - 1][makeAMove[1] - 1] = changeTurn(gameTurnSwitch)
if (gameLogic(turnUserInputToGameGrid) != "Draw") {
printTheGameGrid(turnUserInputToGameGrid)
print(gameLogic(turnUserInputToGameGrid))
break
} else {
print("Draw")
break
}
}
else -> {
numberOfMoves += 1
turnUserInputToGameGrid[makeAMove[0] - 1][makeAMove[1] - 1] = changeTurn(gameTurnSwitch)
if (gameLogic(turnUserInputToGameGrid) == "") {
printTheGameGrid(turnUserInputToGameGrid)
gameTurnSwitch += 1
} else {
printTheGameGrid(turnUserInputToGameGrid)
print(gameLogic(turnUserInputToGameGrid))
break
}
}
}
} catch (e: NumberFormatException) {
println("You should enter numbers!")
}
}
}