gazievDima

TIC TAC TOE

Apr 20th, 2021
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. import java.lang.NumberFormatException
  2.  
  3. var nameUser = "no name"
  4. var nameBot = "bot no name"
  5.  
  6. val NAME_BOTS = arrayOf("Mike", "Alex", "Anna", "Sara", "Lucas")
  7.  
  8. var sizeField = 0
  9.  
  10. var sizeLineForWin = 0
  11.  
  12. var gameField = arrayOf<Array<Char>>()
  13.  
  14. const val POINT: Char = '*'
  15.  
  16. const val FLAG_USER: Char = 'X'
  17. var USER_MOVE_X: Int = 0
  18. var USER_MOVE_Y: Int = 0
  19.  
  20. const val FLAG_BOT: Char = 'O'
  21. var BOT_MOVE_X: Int = 0
  22. var BOT_MOVE_Y: Int = 0
  23.  
  24. fun main() {
  25. welcomeToTheGame()
  26. initGameSettings()
  27. println("Start game!")
  28. startGame()
  29.  
  30. }
  31.  
  32. fun welcomeToTheGame() {
  33. println("Hello, welcome to tic tac toe!")
  34. print("What's your name? Please enter it: ")
  35. nameUser = readLine().toString()
  36. nameBot = NAME_BOTS[(Math.random() * NAME_BOTS.size).toInt()]
  37. println("You opponent is $nameBot")
  38. }
  39.  
  40. fun initGameSettings() {
  41.  
  42. sizeField =
  43. checkInputCorrectData("Enter to size on the field: ",
  44. 0,
  45. 99,
  46. "WARNING!!! Minimal and maximum size game field may be 3 - 99 cells, pls repeat!",
  47. "WARNING!!! You entered incorrect data, pls repeat!")
  48.  
  49. sizeLineForWin =
  50. checkInputCorrectData("Enter the line size for win: ",
  51. 3,
  52. sizeField,
  53. "WARNING!!! Minimal and maximum length line for win 3 - $sizeField, pls repeat!",
  54. "WARNING!!! You entered incorrect data, pls repeat!")
  55.  
  56. gameField = Array(sizeField) { Array(sizeField) { POINT } }
  57. }
  58.  
  59. fun checkInputCorrectData(msgForUser: String,
  60. min: Int,
  61. max: Int,
  62. msgWarningIncorrectData: String,
  63. msgWarningNumberFormatException: String): Int {
  64. var inputUser: Int
  65.  
  66. while (true) {
  67. print(msgForUser)
  68. try {
  69. inputUser = readLine()!!.toInt()
  70. if (inputUser in min..max) {
  71. return inputUser
  72. } else {
  73. println(msgWarningIncorrectData)
  74. }
  75. } catch (e: NumberFormatException) {
  76. println(msgWarningNumberFormatException)
  77. }
  78. }
  79. }
  80.  
  81. fun startGame() {
  82. while (true) {
  83. printField()
  84. if (!playUser()) {
  85. break
  86. }
  87. if (checkForWin(FLAG_USER)) {
  88. printNameWinner(nameUser)
  89. break
  90. }
  91. if (!playBot()) {
  92. break
  93. }
  94. if (checkForWin(FLAG_BOT)) {
  95. printNameWinner(nameBot)
  96. break
  97. }
  98. }
  99. }
  100.  
  101. fun printField() {
  102. for (i in 0..gameField[0].size) {
  103. if (i != 0) {
  104. print("${i}\t")
  105. } else {
  106. print(" \t")
  107. }
  108. }
  109. println()
  110.  
  111. for (i in gameField.indices) {
  112. print("${i + 1}\t")
  113. for (element in gameField[i]) {
  114. print("${element}\t")
  115. }
  116. println()
  117. }
  118. }
  119.  
  120. fun printNameWinner(nameUser: String) {
  121. printField()
  122. println("Game over! $nameUser is winner! ^_^")
  123. }
  124.  
  125. fun checkForWin(FLAG_GAMER: Char): Boolean {
  126. if (searchWinHorizontal(FLAG_GAMER)) return true
  127. if (searchWinVertical(FLAG_GAMER)) return true
  128. if (searchWinDiagonalToRightBottom(FLAG_GAMER)) return true
  129. if (searchWinDiagonalToLeftBottom(FLAG_GAMER)) return true
  130.  
  131. return false
  132. }
  133.  
  134. fun searchWinHorizontal(FLAG_GAMER: Char): Boolean {
  135. var buffer = 0
  136.  
  137. for (i in 0..gameField.size - 1) {
  138.  
  139. for (j in 0..gameField[0].size - 1) {
  140. if (gameField[i][j] != FLAG_GAMER) {
  141. buffer = 0
  142. } else {
  143. buffer += 1
  144. if (buffer == sizeLineForWin) return true
  145. }
  146. }
  147.  
  148. buffer = 0
  149. }
  150. return false
  151. }
  152.  
  153. fun searchWinVertical(FLAG_GAMER: Char): Boolean {
  154. var buffer = 0
  155.  
  156. for (i in 0..gameField[0].size - 1) {
  157.  
  158. for (j in 0..gameField.size - 1) {
  159. if (gameField[j][i] != FLAG_GAMER) {
  160. buffer = 0
  161. } else {
  162. buffer += 1
  163. if (buffer == sizeLineForWin) return true
  164. }
  165. }
  166.  
  167. buffer = 0
  168. }
  169. return false
  170.  
  171. }
  172.  
  173. fun searchWinDiagonalToRightBottom(FLAG_GAMER: Char): Boolean {
  174. var buffer = 0
  175.  
  176. for (i in 0..gameField.size - 1) {
  177. for (j in 0..gameField[0].size - 1) {
  178.  
  179. if (gameField[i][j] == FLAG_GAMER) {
  180. buffer += 1
  181.  
  182. for (n in 1..sizeLineForWin) {
  183.  
  184. if (i + n > gameField.size - 1 || j + n > gameField[0].size - 1) {
  185. buffer = 0
  186. break
  187. } else {
  188.  
  189. if (gameField[i + n][j + n] == FLAG_GAMER) {
  190. buffer += 1
  191. if (buffer == sizeLineForWin) return true
  192. } else {
  193. buffer = 0
  194. break
  195. }
  196. }
  197. }
  198. }
  199. }
  200. buffer = 0
  201. }
  202. return false
  203. }
  204.  
  205. fun searchWinDiagonalToLeftBottom(FLAG_GAMER: Char): Boolean {
  206. var buffer = 0
  207.  
  208. for (i in 0..gameField.size - 1) {
  209. for (j in 0..gameField[0].size - 1) {
  210.  
  211. if (gameField[i][j] == FLAG_GAMER) {
  212. buffer += 1
  213.  
  214. for (n in 1..sizeLineForWin) {
  215.  
  216. if (i + n > gameField.size - 1 || j - n < 0) {
  217. buffer = 0
  218. break
  219. } else {
  220.  
  221. if (gameField[i + n][j - n] == FLAG_GAMER) {
  222. buffer += 1
  223. if (buffer == sizeLineForWin) return true
  224. } else {
  225. buffer = 0
  226. break
  227. }
  228. }
  229. }
  230. }
  231. }
  232. buffer = 0
  233. }
  234. return false
  235. }
  236.  
  237. fun playUser(): Boolean {
  238. return if (!checkForEmptyField()) {
  239. false
  240. } else {
  241.  
  242. USER_MOVE_Y =
  243. checkInputCorrectData("Walk User: Enter to coordinate >> 'X' <<: ",
  244. 0,
  245. sizeField,
  246. "WARNING!!! You entered incorrect data, pls repeat!",
  247. "WARNING!!! You entered incorrect data, pls repeat!")
  248.  
  249. USER_MOVE_X =
  250. checkInputCorrectData("Walk User: Enter to coordinate >> 'Y' <<: ",
  251. 0,
  252. sizeField,
  253. "WARNING!!! You entered incorrect data, pls repeat!",
  254. "WARNING!!! You entered incorrect data, pls repeat!")
  255.  
  256. gameField[USER_MOVE_X - 1][USER_MOVE_Y - 1] = FLAG_USER
  257.  
  258. true
  259. }
  260. }
  261.  
  262. fun playBot(): Boolean {
  263. return if (!checkForEmptyField()) {
  264. false
  265. } else {
  266.  
  267. while (true) {
  268. BOT_MOVE_Y = (Math.random() * sizeField + 1).toInt() - 1
  269. BOT_MOVE_X = (Math.random() * sizeField + 1).toInt() - 1
  270. if (gameField[BOT_MOVE_X][BOT_MOVE_Y] == POINT) {
  271. gameField[BOT_MOVE_X][BOT_MOVE_Y] = FLAG_BOT
  272. break;
  273. }
  274. }
  275.  
  276. true
  277. }
  278. }
  279.  
  280. fun checkForEmptyField(): Boolean {
  281. for (elStr in gameField) {
  282. for (elColumn in elStr) {
  283. if (elColumn == POINT) {
  284. return true
  285. }
  286. }
  287. }
  288.  
  289. printField()
  290. println("Game over. Draw!")
  291. return false
  292. }
  293.  
  294.  
  295.  
  296.  
Advertisement
Add Comment
Please, Sign In to add comment