Guest User

Untitled

a guest
May 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. """Your challenge in this exercise is to use the functions from those previous exercises all together in the same program to make a two-player game that you can play with a friend."""
  2.  
  3. def Board_dim():
  4. """Asks the user for board dimensions"""
  5. x = int(input('How many spaces wide should the board be?\n'))
  6. y = int(input('How high?\n'))
  7. return [x, y]
  8.  
  9. def Print_board(x, y):
  10. """Prints a game board of dimension x by y
  11. Returns the field"""
  12. field = ['', '|', '']
  13. i = 1
  14. j = 1
  15. while i <= x:
  16. field[0] += ' ---'
  17. field[1] += ' |'
  18. field[2] += ' ---'
  19. i += 1
  20. while j < y:
  21. field.append(field[1])
  22. field.append(field[2])
  23. j += 1
  24. if x == 0 or y == 0:
  25. print(' ')
  26. else:
  27. for elem in field:
  28. print(elem)
  29. return field
  30.  
  31. def Print_mark(field, board):
  32. """Marks field from Print_board with board from Alter_board
  33. An X for Player 1 or an O for Player 2
  34. Prints the marked board and return it"""
  35. for i in range(len(board)): # Loop for board rows
  36. for j in range(len(board[i])): # Loop for board columns
  37. if board[i][j] == 'X': # Tanslate board coordinates to field
  38. flst = list(field[2 * i + 1])
  39. flst[4 * j + 2] = 'X'
  40. field[2 * i + 1] = ''.join(flst)
  41. elif board[i][j] == 'O':
  42. flst = list(field[2 * i + 1])
  43. flst[4 * j + 2] = 'O'
  44. field[2 * i + 1] = ''.join(flst)
  45. for elem in field:
  46. print(elem)
  47. return(field)
  48.  
  49. def Check_hor(board):
  50. """Checks whether a player had 3 in a row horizontally"""
  51. win = 0
  52. rowa = board[0]
  53. rowb = board[1]
  54. rowc = board[2]
  55. if rowa.count('X') == 3 or rowb.count('X') == 3 or rowc.count('X') == 3:
  56. win = 1
  57. elif rowa.count('O') == 3 or rowb.count('O') == 3 or rowc.count('O') == 3:
  58. win = 2
  59. return win
  60.  
  61. def Check_ver(board):
  62. """Checks whether a player had 3 in a row vertically"""
  63. win = 0
  64. ver0 = []
  65. ver1 = []
  66. ver2 = []
  67. for i in range(3):
  68. ver0.append(board[i][0])
  69. ver1.append(board[i][1])
  70. ver2.append(board[i][2])
  71. if ver0.count('X') == 3 or ver1.count('X') == 3 or ver2.count('X') == 3:
  72. win = 1
  73. elif ver0.count('O') == 3 or ver1.count('O') == 3 or ver2.count('O') == 3:
  74. win = 2
  75. return win
  76.  
  77. def Check_dia(board):
  78. """Checks whether a player had 3 in a row diagonally"""
  79. win = 0
  80. diaM = [board[i][i] for i in range(3)]
  81. diam = [board[i][2 - i] for i in range(3)]
  82. if diaM.count('X') == 3 or diam.count('X') == 3:
  83. win = 1
  84. elif diaM.count('O') == 3 or diam.count('O') == 3:
  85. win = 2
  86. return win
  87.  
  88. def Coord(place):
  89. """Translate user input x-coord,y-coord string into list [x, y] as integers"""
  90. return [int(x) for x in place.split(',')]
  91.  
  92. def Alter_board(move, board, player):
  93. """Places an X for Player 1, an O for Player 2 in the spot defined by move
  94. Returns the altered board"""
  95. run = True
  96. move = [i - 1 for i in move] # Translates move into indices
  97. while run:
  98. if board[move[0]][move[1]] != 0:
  99. Alter_board(Coord(input('Player %i, enter a valid move:\n' % player)),
  100. board, player)
  101. elif player == 1:
  102. board[move[0]][move[1]] = 'X'
  103. run = False
  104. elif player == 2:
  105. board[move[0]][move[1]] = 'O'
  106. run = False
  107. return board
  108.  
  109. def Play_again(record, x, y, f0, board, num, play):
  110. """Asks whether players want to play again
  111. If yes, resets the board and move count
  112. If not prints their record"""
  113. again = input('Play Again? (y/n)\n')
  114. if again == 'y':
  115. f0 = Print_board(x, y)
  116. board = [[0 for i in range(x)] for j in range(y)]
  117. num = 0
  118. return[f0, board, num, play]
  119. else:
  120. print('Game Record\nP1 wins: {0}\nP2 wins: {1}\nDraws: {2}'.format(record[0], record[1], record[2]))
  121. play = False
  122. return[f0, board, num, play]
  123.  
  124. if __name__=="__main__":
  125. [x, y] = Board_dim()
  126. f0 = Print_board(x, y)
  127. board = [[0 for i in range(x)] for j in range(y)]
  128. num = 0
  129. record = [0, 0, 0]
  130. play = True
  131. print('''Enter move coordinates in the form "row,col"''',
  132. '''For example, "1,1" selects the upper left-hand corner of an n-by-n board''')
  133. while play:
  134. if num == x * y:
  135. if Check_hor(board) == 1 or Check_ver(board) == 1 or Check_dia(board) == 1:
  136. print('Player 1 wins')
  137. else:
  138. print('All spots are occupied\nDraw')
  139. record[2] += 1
  140. [f0, board, num, play] = Play_again(record, x, y, f0, board, num, play)
  141. elif num % 2 == 0:
  142. board = Alter_board(Coord(input('Player 1, your move:\n')),
  143. board, player = 1)
  144. num += 1
  145. f0 = Print_mark(f0, board)
  146. if Check_hor(board) == 1 or Check_ver(board) == 1 or Check_dia(board) == 1:
  147. print('Player 1 wins')
  148. record[0] +=1
  149. [f0, board, num, play] = Play_again(record, x, y, f0, board, num, play)
  150. else:
  151. board = Alter_board(Coord(input('Player 2, your move:\n')),
  152. board, player = 2)
  153. num += 1
  154. f0 = Print_mark(f0, board)
  155. if Check_hor(board) == 2 or Check_ver(board) == 2 or Check_dia(board) == 2:
  156. print('Player 2 wins')
  157. record[1] += 1
  158. [f0, board, num, play] = Play_again(record, x, y, f0, board, num, play)
  159.  
  160. """Notes for improvements: Modify so that Players 1 and 2 alternate the first move"""
Add Comment
Please, Sign In to add comment