Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.NumberFormatException
- var nameUser = "no name"
- var nameBot = "bot no name"
- val NAME_BOTS = arrayOf("Mike", "Alex", "Anna", "Sara", "Lucas")
- var sizeField = 0
- var sizeLineForWin = 0
- var gameField = arrayOf<Array<Char>>()
- const val POINT: Char = '*'
- const val FLAG_USER: Char = 'X'
- var USER_MOVE_X: Int = 0
- var USER_MOVE_Y: Int = 0
- const val FLAG_BOT: Char = 'O'
- var BOT_MOVE_X: Int = 0
- var BOT_MOVE_Y: Int = 0
- fun main() {
- welcomeToTheGame()
- initGameSettings()
- println("Start game!")
- startGame()
- }
- fun welcomeToTheGame() {
- println("Hello, welcome to tic tac toe!")
- print("What's your name? Please enter it: ")
- nameUser = readLine().toString()
- nameBot = NAME_BOTS[(Math.random() * NAME_BOTS.size).toInt()]
- println("You opponent is $nameBot")
- }
- fun initGameSettings() {
- sizeField =
- checkInputCorrectData("Enter to size on the field: ",
- 0,
- 99,
- "WARNING!!! Minimal and maximum size game field may be 3 - 99 cells, pls repeat!",
- "WARNING!!! You entered incorrect data, pls repeat!")
- sizeLineForWin =
- checkInputCorrectData("Enter the line size for win: ",
- 3,
- sizeField,
- "WARNING!!! Minimal and maximum length line for win 3 - $sizeField, pls repeat!",
- "WARNING!!! You entered incorrect data, pls repeat!")
- gameField = Array(sizeField) { Array(sizeField) { POINT } }
- }
- fun checkInputCorrectData(msgForUser: String,
- min: Int,
- max: Int,
- msgWarningIncorrectData: String,
- msgWarningNumberFormatException: String): Int {
- var inputUser: Int
- while (true) {
- print(msgForUser)
- try {
- inputUser = readLine()!!.toInt()
- if (inputUser in min..max) {
- return inputUser
- } else {
- println(msgWarningIncorrectData)
- }
- } catch (e: NumberFormatException) {
- println(msgWarningNumberFormatException)
- }
- }
- }
- fun startGame() {
- while (true) {
- printField()
- if (!playUser()) {
- break
- }
- if (checkForWin(FLAG_USER)) {
- printNameWinner(nameUser)
- break
- }
- if (!playBot()) {
- break
- }
- if (checkForWin(FLAG_BOT)) {
- printNameWinner(nameBot)
- break
- }
- }
- }
- fun printField() {
- for (i in 0..gameField[0].size) {
- if (i != 0) {
- print("${i}\t")
- } else {
- print(" \t")
- }
- }
- println()
- for (i in gameField.indices) {
- print("${i + 1}\t")
- for (element in gameField[i]) {
- print("${element}\t")
- }
- println()
- }
- }
- fun printNameWinner(nameUser: String) {
- printField()
- println("Game over! $nameUser is winner! ^_^")
- }
- fun checkForWin(FLAG_GAMER: Char): Boolean {
- if (searchWinHorizontal(FLAG_GAMER)) return true
- if (searchWinVertical(FLAG_GAMER)) return true
- if (searchWinDiagonalToRightBottom(FLAG_GAMER)) return true
- if (searchWinDiagonalToLeftBottom(FLAG_GAMER)) return true
- return false
- }
- fun searchWinHorizontal(FLAG_GAMER: Char): Boolean {
- var buffer = 0
- for (i in 0..gameField.size - 1) {
- for (j in 0..gameField[0].size - 1) {
- if (gameField[i][j] != FLAG_GAMER) {
- buffer = 0
- } else {
- buffer += 1
- if (buffer == sizeLineForWin) return true
- }
- }
- buffer = 0
- }
- return false
- }
- fun searchWinVertical(FLAG_GAMER: Char): Boolean {
- var buffer = 0
- for (i in 0..gameField[0].size - 1) {
- for (j in 0..gameField.size - 1) {
- if (gameField[j][i] != FLAG_GAMER) {
- buffer = 0
- } else {
- buffer += 1
- if (buffer == sizeLineForWin) return true
- }
- }
- buffer = 0
- }
- return false
- }
- fun searchWinDiagonalToRightBottom(FLAG_GAMER: Char): Boolean {
- var buffer = 0
- for (i in 0..gameField.size - 1) {
- for (j in 0..gameField[0].size - 1) {
- if (gameField[i][j] == FLAG_GAMER) {
- buffer += 1
- for (n in 1..sizeLineForWin) {
- if (i + n > gameField.size - 1 || j + n > gameField[0].size - 1) {
- buffer = 0
- break
- } else {
- if (gameField[i + n][j + n] == FLAG_GAMER) {
- buffer += 1
- if (buffer == sizeLineForWin) return true
- } else {
- buffer = 0
- break
- }
- }
- }
- }
- }
- buffer = 0
- }
- return false
- }
- fun searchWinDiagonalToLeftBottom(FLAG_GAMER: Char): Boolean {
- var buffer = 0
- for (i in 0..gameField.size - 1) {
- for (j in 0..gameField[0].size - 1) {
- if (gameField[i][j] == FLAG_GAMER) {
- buffer += 1
- for (n in 1..sizeLineForWin) {
- if (i + n > gameField.size - 1 || j - n < 0) {
- buffer = 0
- break
- } else {
- if (gameField[i + n][j - n] == FLAG_GAMER) {
- buffer += 1
- if (buffer == sizeLineForWin) return true
- } else {
- buffer = 0
- break
- }
- }
- }
- }
- }
- buffer = 0
- }
- return false
- }
- fun playUser(): Boolean {
- return if (!checkForEmptyField()) {
- false
- } else {
- USER_MOVE_Y =
- checkInputCorrectData("Walk User: Enter to coordinate >> 'X' <<: ",
- 0,
- sizeField,
- "WARNING!!! You entered incorrect data, pls repeat!",
- "WARNING!!! You entered incorrect data, pls repeat!")
- USER_MOVE_X =
- checkInputCorrectData("Walk User: Enter to coordinate >> 'Y' <<: ",
- 0,
- sizeField,
- "WARNING!!! You entered incorrect data, pls repeat!",
- "WARNING!!! You entered incorrect data, pls repeat!")
- gameField[USER_MOVE_X - 1][USER_MOVE_Y - 1] = FLAG_USER
- true
- }
- }
- fun playBot(): Boolean {
- return if (!checkForEmptyField()) {
- false
- } else {
- while (true) {
- BOT_MOVE_Y = (Math.random() * sizeField + 1).toInt() - 1
- BOT_MOVE_X = (Math.random() * sizeField + 1).toInt() - 1
- if (gameField[BOT_MOVE_X][BOT_MOVE_Y] == POINT) {
- gameField[BOT_MOVE_X][BOT_MOVE_Y] = FLAG_BOT
- break;
- }
- }
- true
- }
- }
- fun checkForEmptyField(): Boolean {
- for (elStr in gameField) {
- for (elColumn in elStr) {
- if (elColumn == POINT) {
- return true
- }
- }
- }
- printField()
- println("Game over. Draw!")
- return false
- }
Advertisement
Add Comment
Please, Sign In to add comment