└── Tic-Tac-Toe └── task └── src └── tictactoe └── Main.kt /Tic-Tac-Toe/task/src/tictactoe/Main.kt: -------------------------------------------------------------------------------- 1 | package tictactoe 2 | 3 | import java.util.* 4 | /* 5 | Work on project. Stage 5/5: Fight! 6 | */ 7 | fun main() { 8 | val matrix = Matrix(3,3) 9 | matrix.printFields() 10 | while (!matrix.isGameEnds) { 11 | matrix.readCoordinates() 12 | matrix.changePlayer() 13 | matrix.printFields() 14 | matrix.checkState() 15 | } 16 | print(matrix.getState()) 17 | } 18 | 19 | class Matrix { 20 | var x: Int = 0 21 | var y: Int = 0 22 | val m: Array 23 | var player = 1 24 | var straightLine = 0 25 | 26 | var isWinsX = false 27 | var isWinsO = false 28 | var hasEmptyCells = false 29 | var isItImposible = false 30 | 31 | var isGameEnds = false 32 | 33 | fun checkState() { 34 | sumMatrix() 35 | checkRows() 36 | checkColumns() 37 | checkAscDiagonal() 38 | checkDescDiagonal() 39 | if (isItImposible || isWinsX || isWinsO || !hasEmptyCells) isGameEnds = true 40 | } 41 | 42 | fun getState(): String { 43 | if (isItImposible) return "Impossible" 44 | if (isWinsX) return "X wins" 45 | if (isWinsO) return "O wins" 46 | if (!hasEmptyCells) return "Draw" 47 | return "Game not finished" 48 | } 49 | 50 | fun readCoordinates() { 51 | var x = 0 52 | var y = 0 53 | while (true) { 54 | print("Enter the coordinates: ") 55 | val scanner = Scanner(System.`in`) 56 | val input = scanner.nextLine().split(" ") 57 | 58 | if (input.size != 2) print("You should enter numbers!\n") 59 | else if (input[0].length == 1 && input[1].length == 1) { 60 | if (input[0].first().isDigit() && input[1].first().isDigit()) { 61 | x = input[1].toInt() 62 | y = input[0].toInt() 63 | if (x in 1..3 && y in 1..3) break 64 | print("Coordinates should be from 1 to 3!\n") 65 | continue 66 | } 67 | } 68 | } 69 | addMove(x - 1, y - 1) 70 | } 71 | 72 | private fun addMove(x: Int, y: Int) { 73 | if (getElenent(y,x) == 0) m[y][x] = player 74 | else { 75 | print("This cell is occupied! Choose another one!\n") 76 | readCoordinates() 77 | } 78 | } 79 | 80 | fun changePlayer() { 81 | player = if (player == 1) -1 else 1 82 | } 83 | 84 | fun sumMatrix() { 85 | hasEmptyCells = false 86 | var sum = 0 87 | var currentElement = 0 88 | for (i in 0 until y) { 89 | for (j in 0 until x) { 90 | currentElement = getElenent(i, j) 91 | sum += currentElement 92 | if (currentElement == 0) hasEmptyCells = true 93 | } 94 | } 95 | if (sum > 1 || sum < -1) isItImposible = true 96 | } 97 | 98 | fun checkRows() { 99 | for (i in 0 until x) { 100 | straightLine = checkRow(i) 101 | if (straightLine == 3) isWinsX = true 102 | if (straightLine == -3) isWinsO = true 103 | } 104 | } 105 | 106 | fun checkColumns() { 107 | for (i in 0 until y) { 108 | straightLine = checkColumn(i) 109 | if (straightLine == 3) isWinsX = true 110 | if (straightLine == -3) isWinsO = true 111 | } 112 | } 113 | 114 | fun checkAscDiagonal() { 115 | straightLine = getElenent(2, 0) + getElenent(1, 1) + getElenent(0, 2) 116 | if (straightLine == 3) isWinsX = true 117 | if (straightLine == -3) isWinsO = true 118 | } 119 | 120 | fun checkDescDiagonal() { 121 | straightLine = getElenent(0, 0) + getElenent(1, 1) + getElenent(2, 2) 122 | if (straightLine == 3) isWinsX = true 123 | if (straightLine == -3) isWinsO = true 124 | } 125 | 126 | fun checkRow(x: Int) = getElenent(x, 0) + getElenent(x, 1) + getElenent(x, 2) 127 | 128 | fun checkColumn(y: Int) = getElenent(0, y) + getElenent(1, y) + getElenent(2, y) 129 | 130 | fun getElenent(row: Int, column: Int) = m[row][column] 131 | 132 | fun printFields() { 133 | var output = "---------\n" 134 | for (y in 0..2){ 135 | output += "| " 136 | for (x in 0..2) { 137 | output += when (getElenent(y, x)) { 138 | 1 -> "X " 139 | -1 -> "O " 140 | else -> " " 141 | } 142 | } 143 | output += "|\n" 144 | } 145 | output += "---------\n" 146 | print(output) 147 | } 148 | 149 | constructor (x: Int, y: Int) { 150 | this.x = x 151 | this.y = y 152 | m = Array(x) { IntArray(y) } 153 | } 154 | } 155 | 156 | 157 | 158 | 159 | /* Stage 1-5 Welcome to the battlefield 160 | print("X O X\n" + 161 | "O X O\n" + 162 | "X X O ") 163 | */ 164 | 165 | 166 | 167 | 168 | /* Stage 2/5: The user is the gamemaster 169 | val scanner = Scanner(System.`in`) 170 | val input = scanner.next().split("") 171 | val matrix = Matrix(3,3) 172 | var n = 1 173 | for (y in 0..2){ 174 | for (x in 0..2) { 175 | matrix.m[x][y] = when (input[n++]) { 176 | "X" -> 1 177 | "O" -> 2 178 | else -> 0 179 | } 180 | } 181 | } 182 | var output = "---------\n" 183 | for (y in 0..2){ 184 | output += "| " 185 | for (x in 0..2) { 186 | output += when (matrix.m[x][y]) { 187 | 1 -> "X " 188 | 2 -> "O " 189 | else -> "_ " 190 | } 191 | } 192 | output += "|\n" 193 | } 194 | output += "---------" 195 | print(output) 196 | } 197 | class Matrix { 198 | var x: Int = 0 199 | var y: Int = 0 200 | val m: Array 201 | constructor( 202 | x: Int, 203 | y: Int 204 | ) { 205 | this.x = x 206 | this.y = y 207 | m = Array(x) { IntArray(y) } 208 | for (i in 0 until x) { 209 | for (j in 0 until y) 210 | m[i][j] = 0 211 | } 212 | } 213 | } 214 | */ 215 | 216 | 217 | 218 | 219 | /* 220 | Stage 3/5: What's up on the field? 221 | fun main() { 222 | val matrix = readInput() 223 | matrix.printFields() 224 | print(matrix.state()) 225 | } 226 | fun readInput(): Matrix { 227 | print("Enter cells: ") 228 | val scanner = Scanner(System.`in`) 229 | val input = scanner.next().split("") 230 | val matrix = Matrix(3,3) 231 | var n = 1 232 | for (y in 0..2){ 233 | for (x in 0..2) { 234 | matrix.m[y][x] = when (input[n++]) { 235 | "X" -> 1 236 | "O" -> -1 237 | else -> 0 238 | } 239 | } 240 | } 241 | return matrix 242 | } 243 | class Matrix { 244 | var x: Int = 0 245 | var y: Int = 0 246 | val m: Array 247 | var isItImposible = false 248 | var isWinsX = false 249 | var isWinsO = false 250 | var hasEmptyCells = false 251 | fun state(): String { 252 | sumMatrix() 253 | checkRows() 254 | checkColumns() 255 | checkAscDiagonal()Stage 3/5: What's up on the field 256 | checkDescDiagonal() 257 | checkDoubleWiners() 258 | if (isItImposible) return "Impossible" 259 | if (isWinsX) return "X wins" 260 | if (isWinsO) return "O wins" 261 | if (!hasEmptyCells) return "Draw" 262 | if (hasEmptyCells) return "Game not finished" 263 | return "Impossible @*&%#!!!" 264 | } 265 | fun sumMatrix() { 266 | var sum = 0 267 | var currentElement = 0 268 | for (i in 0 until y) { 269 | for (j in 0 until x) { 270 | currentElement = getElenent(i, j) 271 | sum += currentElement 272 | if (currentElement == 0) hasEmptyCells = true 273 | } 274 | } 275 | if (sum > 1 || sum < -1) isItImposible = true 276 | } 277 | fun getElenent(row: Int, column: Int) = m[row][column] 278 | fun checkRow(x: Int) = getElenent(x, 0) + getElenent(x, 1) + getElenent(x, 2) 279 | fun checkColumn(y: Int) = getElenent(0, y) + getElenent(1, y) + getElenent(2, y) 280 | fun checkRows() { 281 | var whoseRow = 0 282 | for (i in 0 until x) { 283 | whoseRow = checkRow(i) 284 | if (whoseRow == 3) isWinsX = true 285 | if (whoseRow == -3) isWinsO = true 286 | } 287 | } 288 | fun checkColumns() { 289 | var whoseColumn = 0 290 | for (i in 0 until y) { 291 | whoseColumn = checkColumn(i) 292 | if (whoseColumn == 3) isWinsX = true 293 | if (whoseColumn == -3) isWinsO = true 294 | } 295 | } 296 | /* 297 | XXX 298 | OXO 299 | OOX 300 | */ 301 | fun checkAscDiagonal() { 302 | val whoseDiagonal = getElenent(2, 0) + getElenent(1, 1) + getElenent(0, 2) 303 | if (whoseDiagonal == 3 /*&& !isWinsX*/) isWinsX = true 304 | if (whoseDiagonal == -3/* && !isWinsO*/) isWinsO = true 305 | } 306 | fun checkDescDiagonal() { 307 | val whoseDiagonal = getElenent(0, 0) + getElenent(1, 1) + getElenent(2, 2) 308 | if (whoseDiagonal == 3 /*&& !isWinsX*/) isWinsX = true 309 | if (whoseDiagonal == -3 /*&& !isWinsO*/) isWinsO = true 310 | } 311 | private fun checkDoubleWiners() { 312 | if (isWinsX && isWinsO) isItImposible = true 313 | } 314 | fun printFields() { 315 | var output = "---------\n" 316 | for (y in 0..2){ 317 | output += "| " 318 | for (x in 0..2) { 319 | output += when (getElenent(y, x)) { 320 | 1 -> "X " 321 | -1 -> "O " 322 | else -> "_ " 323 | } 324 | } 325 | output += "|\n" 326 | } 327 | output += "---------\n" 328 | print(output) 329 | } 330 | constructor ( 331 | x: Int, 332 | y: Int 333 | ) { 334 | this.x = x 335 | this.y = y 336 | m = Array(x) { IntArray(y) } 337 | for (i in 0 until x) { 338 | for (j in 0 until y) m[i][j] = 0 339 | } 340 | } 341 | } 342 | */ 343 | 344 | 345 | 346 | 347 | /* 348 | Stage 4/5: First move! 349 | 350 | fun main() { 351 | val matrix = readInput() 352 | matrix.printFields() 353 | // print(matrix.state()) 354 | matrix.readTurn(1) 355 | matrix.printFields() 356 | } 357 | 358 | fun readInput(): Matrix { 359 | print("Enter cells: ") 360 | val scanner = Scanner(System.`in`) 361 | val input = scanner.next().split("") 362 | 363 | val matrix = Matrix(3,3) 364 | var n = 1 365 | for (y in 0..2){ 366 | for (x in 0..2) { 367 | matrix.m[y][x] = when (input[n++]) { 368 | "X" -> 1 369 | "O" -> -1 370 | else -> 0 371 | } 372 | } 373 | } 374 | return matrix 375 | } 376 | 377 | class Matrix { 378 | var x: Int = 0 379 | var y: Int = 0 380 | val m: Array 381 | 382 | var isItImposible = false 383 | var isWinsX = false 384 | var isWinsO = false 385 | var hasEmptyCells = false 386 | 387 | fun state(): String { 388 | sumMatrix() 389 | checkRows() 390 | checkColumns() 391 | checkAscDiagonal() 392 | checkDescDiagonal() 393 | checkDoubleWiners() 394 | 395 | if (isItImposible) return "Impossible" 396 | if (isWinsX) return "X wins" 397 | if (isWinsO) return "O wins" 398 | if (!hasEmptyCells) return "Draw" 399 | if (hasEmptyCells) return "Game not finished" 400 | return "Impossible @*&%#!!!" 401 | } 402 | 403 | fun readTurn(player: Int) { 404 | var x = 0 405 | var y = 0 406 | while (true) { 407 | print("Enter the coordinates: ") 408 | val scanner = Scanner(System.`in`) 409 | val input = scanner.nextLine().split(" ") 410 | 411 | if (input.size == 2) { 412 | if (input[0].length == 1 && input[1].length == 1) { 413 | if (input[0].first().isDigit() && input[1].first().isDigit()) { 414 | x = input[1].toInt() 415 | y = input[0].toInt() 416 | if (y in 1..3 && x in 1..3) break 417 | print("Coordinates should be from 1 to 3!\n") 418 | continue 419 | } 420 | } 421 | } 422 | print("You should enter numbers!\n") 423 | } 424 | addMove(x - 1, y - 1, player) 425 | } 426 | 427 | private fun addMove(x: Int, y: Int, player: Int) { 428 | if (getElenent(y,x) == 0) m[y][x] = player 429 | else { 430 | print("This cell is occupied! Choose another one!\n") 431 | readTurn(player) 432 | } 433 | } 434 | 435 | fun sumMatrix() { 436 | var sum = 0 437 | var currentElement = 0 438 | for (i in 0 until y) { 439 | for (j in 0 until x) { 440 | currentElement = getElenent(i, j) 441 | sum += currentElement 442 | if (currentElement == 0) hasEmptyCells = true 443 | } 444 | } 445 | if (sum > 1 || sum < -1) isItImposible = true 446 | } 447 | 448 | fun getElenent(row: Int, column: Int) = m[row][column] 449 | 450 | fun checkRow(x: Int) = getElenent(x, 0) + getElenent(x, 1) + getElenent(x, 2) 451 | 452 | fun checkColumn(y: Int) = getElenent(0, y) + getElenent(1, y) + getElenent(2, y) 453 | 454 | fun checkRows() { 455 | var whoseRow = 0 456 | for (i in 0 until x) { 457 | whoseRow = checkRow(i) 458 | if (whoseRow == 3) isWinsX = true 459 | if (whoseRow == -3) isWinsO = true 460 | } 461 | } 462 | 463 | fun checkColumns() { 464 | var whoseColumn = 0 465 | for (i in 0 until y) { 466 | whoseColumn = checkColumn(i) 467 | if (whoseColumn == 3) isWinsX = true 468 | if (whoseColumn == -3) isWinsO = true 469 | } 470 | } 471 | 472 | fun checkAscDiagonal() { 473 | val whoseDiagonal = getElenent(2, 0) + getElenent(1, 1) + getElenent(0, 2) 474 | if (whoseDiagonal == 3) isWinsX = true 475 | if (whoseDiagonal == -3) isWinsO = true 476 | } 477 | 478 | fun checkDescDiagonal() { 479 | val whoseDiagonal = getElenent(0, 0) + getElenent(1, 1) + getElenent(2, 2) 480 | if (whoseDiagonal == 3 /*&& !isWinsX*/) isWinsX = true 481 | if (whoseDiagonal == -3 /*&& !isWinsO*/) isWinsO = true 482 | } 483 | 484 | private fun checkDoubleWiners() { 485 | if (isWinsX && isWinsO) isItImposible = true 486 | } 487 | 488 | fun printFields() { 489 | var output = "---------\n" 490 | for (y in 0..2){ 491 | output += "| " 492 | for (x in 0..2) { 493 | output += when (getElenent(y, x)) { 494 | 1 -> "X " 495 | -1 -> "O " 496 | else -> " " 497 | } 498 | } 499 | output += "|\n" 500 | } 501 | output += "---------\n" 502 | print(output) 503 | } 504 | 505 | constructor (x: Int, y: Int) { 506 | this.x = x 507 | this.y = y 508 | m = Array(x) { IntArray(y) } 509 | } 510 | } 511 | */ --------------------------------------------------------------------------------