Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. # Tic-Tac-Toe
  2. row0 = ["", "", ""]
  3. row1 = ["", "", ""]
  4. row2 = ["", "", ""]
  5.  
  6. currentPlayer = "x"
  7. done = False
  8.  
  9. def printBoard():
  10. global currentPlayer
  11. global done
  12. # print current player turn and game board
  13. print("Current Player: " + currentPlayer)
  14. print(" -0-1-2-")
  15. print("0: |" + row0[0] + "|" + row0[1] + "|" + row0[2] + "|")
  16. print(" -------")
  17. print("1: |" + row1[0] + "|" + row1[1] + "|" + row1[2] + "|")
  18. print(" -------")
  19. print("2: |" + row2[0] + "|" + row2[1] + "|" + row2[2] + "|")
  20. print(" -------")
  21. print("")
  22.  
  23. # get row and column for current player
  24. inputRow = int(input("Enter player " + currentPlayer + " row: "))
  25. inputCol = int(input("Enter player " + currentPlayer + " col: "))
  26.  
  27. # place mark in appropriate cell
  28. if (inputRow == 0):
  29. row0[inputCol] = currentPlayer
  30. elif (inputRow == 1):
  31. row1[inputCol] = currentPlayer
  32. elif (inputRow == 2):
  33. row2[inputCol] = currentPlayer
  34. else:
  35. done == True
  36.  
  37. # escape from loop on invalid row
  38.  
  39. # Create an “if” block that will change the player.
  40. if currentPlayer == "x":
  41. currentPlayer = "0"
  42.  
  43. else:
  44. currentPlayer = "x"
  45. return
  46. # Check to see if there is 3-in-a-row in any direction.
  47. # There are 8 possibilities, so write code that will manually check each one for the samemarks in a row.
  48. # If 3 in a row is found, print a winning message and set a variable to “True” to escape loop.
  49. #The first row and column has been done for you. Complete the remaining rows and columns.
  50. #Check the diagonals for 3 in a diagonal too. The first one has been done for you.
  51. def CheckRows():
  52. global done
  53.  
  54. # row 0
  55. if ((row0[0] == "X") and (row0[1] == "X") and (row0[2] == "X")):
  56. print("Player X wins in row 0!")
  57. done = True
  58. if ((row0[0] == "o") and (row0[1] == "o") and (row0[2] == "o")):
  59. print("Player o wins in row 0!")
  60. done = True
  61.  
  62. # row 1
  63. if ((row1[0] == "X") and (row1[1] == "X") and (row1[2] == "X")):
  64. print("Player X wins in row 1!")
  65. done = True
  66. if ((row1[0] == "o") and (row1[1] == "o") and (row1[2] == "o")):
  67. print("Player o wins in row 1!")
  68. done = True
  69.  
  70.  
  71. # row 2
  72.  
  73.  
  74. if ((row2[0] == "X") and (row2[1] == "X") and (row2[2] == "X")):
  75. print("Player X wins in row 2!")
  76. done = True
  77. if ((row2[0] == "o") and (row2[1] == "o") and (row2[2] == "o")):
  78. print("Player o wins in row 2!")
  79. done = True
  80.  
  81. return
  82. def CheckCols():
  83. global done
  84.  
  85. # column 0
  86. if ((row0[0] == "X") and (row1[1] == "X") and (row2[2] == "X")):
  87. print("Player X wins in column 0!")
  88. done = True
  89. if ((row0[0] == "o") and (row1[1] == "o") and (row2[2] == "o")):
  90. print("Player o wins in column 0!")
  91. done = True
  92.  
  93. # column 1
  94.  
  95. if ((row1[0] == "X") and (row1[1] == "X") and (row1[2] == "X")):
  96. print("Player X wins in row 0!")
  97. done = True
  98. if ((row1[0] == "o") and (row1[1] == "o") and (row1[2] == "o")):
  99. print("Player o wins in row 0!")
  100. done = True
  101.  
  102.  
  103.  
  104. # column 2
  105.  
  106.  
  107. if ((row2[0] == "X") and (row2[1] == "X") and (row2[2] == "X")):
  108. print("Player X wins in row 0!")
  109. done = True
  110. if ((row2[0] == "o") and (row2[1] == "o") and (row2[2] == "o")):
  111. print("Player o wins in row 0!")
  112. done = True
  113.  
  114. return
  115. def CheckDiagonal():
  116. global done
  117.  
  118. # first diagonal
  119. if ((row0[0] == "X") and (row1[1] == "X") and (row2[2] == "X")):
  120. print("Player X wins in first diagonal!")
  121. done = True
  122. if ((row0[0] == "o") and (row1[1] == "o") and (row2[2] == "o")):
  123. print("Player o wins in first diagonal!")
  124. done = True
  125. # second diagonal
  126. if ((row0[2] == "X") and (row1[1] == "X") and (row2[0] == "X")):
  127. print("Player X wins in first diagonal!")
  128. done = True
  129. if ((row0[2] == "o") and (row1[1] == "o") and (row2[0] == "o")):
  130. print("Player o wins in first diagonal!")
  131. done = True
  132. return
  133.  
  134. #create game board
  135.  
  136.  
  137. # game board is made up of 3 lists containing 3 elements each
  138. # every element in the list starts out as a blank space, and may gain an "X" or "O" as the user plays the game.
  139. # Initialize a current player variable
  140. # Create a “counter or variable” that should become “True” when the game is over, butremains “False” until it is over
  141.  
  142.  
  143.  
  144. # Create a loop that runs until the game is over using a “while loop”.
  145. while True:
  146. if not done:
  147. printBoard()
  148. CheckRows()
  149. CheckCols()
  150. CheckDiagonal()
  151.  
  152. print(" -0-1-2-")
  153. print("0: |" + row0[0] + "|" + row0[1] + "|" + row0[2] + "|")
  154. print("1: |" + row0[0] + "|" + row0[1] + "|" + row0[2] + "|")
  155. print("2: |" + row0[0] + "|" + row0[1] + "|" + row0[2] + "|")
  156. print(" -------")
  157. print("")
  158. print("GAME OVER")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement