John5617

How to Make a Move in Connect Four Game

Jan 22nd, 2021 (edited)
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.90 KB | None | 0 0
  1. # full code in answer to:
  2. # https://stackoverflow.com/questions/65851299/how-to-make-a-move-in-connect-four-game-please-see-code-below/65853247#65853247
  3.  
  4. import random
  5.  
  6.  
  7. def winner(board):
  8.     """This function accepts the Connect Four board as a parameter.
  9.    If there is no winner, the function will return the empty string "".
  10.    If the user has won, it will return 'X', and if the computer has
  11.    won it will return 'O'."""
  12.  
  13.  
  14.  
  15.     # No winner: return the empty string
  16.     return ""
  17.  
  18.  
  19. def display_board(board):
  20.     """This function accepts the Connect Four board as a parameter.
  21.    It will print the Connect Four board grid (using ASCII characters)
  22.    and show the positions of any X's and O's.  It also displays
  23.    the column numbers on top of the board to help
  24.    the user figure out the coordinates of their next move.
  25.    This function does not return anything."""
  26.  
  27.     print("   0   1   2   3   4   5   6")
  28.     print("   " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " | " + board[0][3] + " | " + board[0][
  29.         4] + " | " + board[0][5] + " | " + board[0][6])
  30.     print("  ---+---+---+---+---+---+---")
  31.     print("   " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " | " + board[1][3] + " | " + board[1][
  32.         4] + " | " + board[1][5] + " | " + board[1][6])
  33.     print("  ---+---+---+---+---+---+---")
  34.     print("   " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " | " + board[2][3] + " | " + board[2][
  35.         4] + " | " + board[2][5] + " | " + board[2][6])
  36.     print("  ---+---+---+---+---+---+---")
  37.     print("   " + board[3][0] + " | " + board[3][1] + " | " + board[3][2] + " | " + board[3][3] + " | " + board[3][
  38.         4] + " | " + board[3][5] + " | " + board[3][6])
  39.     print("  ---+---+---+---+---+---+---")
  40.     print("   " + board[4][0] + " | " + board[4][1] + " | " + board[4][2] + " | " + board[4][3] + " | " + board[4][
  41.         4] + " | " + board[4][5] + " | " + board[4][6])
  42.     print("  ---+---+---+---+---+---+---")
  43.     print("   " + board[5][0] + " | " + board[5][1] + " | " + board[5][2] + " | " + board[5][3] + " | " + board[5][
  44.         4] + " | " + board[5][5] + " | " + board[5][6])
  45.     print("  ---+---+---+---+---+---+---")
  46.     print("   " + board[6][0] + " | " + board[6][1] + " | " + board[6][2] + " | " + board[6][3] + " | " + board[6][
  47.         4] + " | " + board[6][5] + " | " + board[6][6])
  48.     print()
  49.  
  50.  
  51. def make_user_move(board):
  52.     """This function accepts the Connect Four board as a parameter.
  53.    It will ask the user for a row and column.  If the row and
  54.    column are each within the range of 0 and 6, and that square
  55.    is not already occupied, then it will place an 'X' in that square."""
  56.  
  57.     valid_move = False
  58.     while not valid_move:
  59.         col = int(input("What col would you like to move to (0-6):"))
  60.         if board[0][col] != ' ':
  61.             print("Sorry, that column is full. Please try again!\n")
  62.         else:
  63.             for row in range(6, -1, -1):
  64.                 if board[row][col] == ' ' and not valid_move:
  65.                     board[row][col] = 'X'
  66.                     valid_move = True
  67.     return board
  68.  
  69. def make_computer_move(board):
  70.     """This function accepts the Connect Four board as a parameter.
  71.    It will randomly pick row and column values between 0 and 6.
  72.    If that square is not already occupied it will place an 'O'
  73.    in that square.  Otherwise, another random row and column
  74.    will be generated."""
  75.     computer_valid_move = False
  76.     while not computer_valid_move:
  77.         col = random.randint(0, 6)
  78.         if board[0][col] != ' ':
  79.             print("Sorry, that column is full. Please try again!\n")
  80.         else:
  81.             for row in range(6, -1, -1):
  82.                 if board[row][col] == ' ' and not computer_valid_move:
  83.                     board[row][col] = 'O'
  84.                     computer_valid_move = True
  85.     return board
  86.  
  87.  
  88. def main():
  89.     """The Main Game Loop:"""
  90.  
  91.     free_cells = 42
  92.     users_turn = True
  93.     ttt_board = [[" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
  94.                  [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
  95.                  [" ", " ", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " "],
  96.                  [" ", " ", " ", " ", " ", " ", " "]]
  97.  
  98.     while not winner(ttt_board) and (free_cells > 0):
  99.         display_board(ttt_board)
  100.         if users_turn:
  101.             ttt_board = make_user_move(ttt_board)
  102.             users_turn = not users_turn
  103.         else:
  104.             ttt_board = make_computer_move(ttt_board)
  105.             users_turn = not users_turn
  106.         free_cells -= 1
  107.  
  108.     display_board(ttt_board)
  109.     if (winner(ttt_board) == 'X'):
  110.         print("Y O U   W O N !")
  111.     elif (winner(ttt_board) == 'O'):
  112.         print("I   W O N !")
  113.     else:
  114.         print("S T A L E M A T E !")
  115.     print("\n*** GAME OVER ***\n")
  116.  
  117.  
  118. # Start the game!
  119. main()
  120.  
Add Comment
Please, Sign In to add comment