Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.83 KB | None | 0 0
  1. #COPYRIGHT 2019 THEWILDTHORN
  2. class Connect4:
  3.  
  4.     def __init__(self, width, height, window=None): # Initializes the game by setting the parameters for the board and the data that will be stored in the board (or list of lists)
  5.         self.width = width
  6.         self.height = height
  7.         self.data = []
  8.         for row in range(self.height): # This for statement creates the list of lists represented by self.data
  9.             boardRow = []
  10.             for col in range(self.width):
  11.                 boardRow += [' ']
  12.             self.data += [boardRow]
  13.  
  14.     def __repr__(self): # Sets the literal structure of the board so that it's easy to interpret for the players
  15.         s = ''
  16.         for row in range(self.height):
  17.             s += '|'
  18.             for col in range(self.width):
  19.                 s += self.data[row][col] + '|'
  20.             s += '\n'
  21.         s += '--'*self.width + '-\n'
  22.         for col in range(self.width):
  23.             s += ' ' + str(col%10)
  24.         s += '\n'
  25.         return s
  26.  
  27.     def clear(self): # Clears the current state of the board
  28.         for row in range(self.height):
  29.             for col in range(self.width): # For every value in the board
  30.                 self.data[row][col] = ' ' # Override whatever is there with a space (the original value each spot starts with)
  31.  
  32.     def addMove(self, col, ox): # Adds a "checker" to the column passed
  33.         if self.allowsMove(col): # Uses allowsMove to check if the column is available
  34.             for row in range(self.height):
  35.                 if self.data[row][col] != ' ': # If the spot is not empty...
  36.                     self.data[row-1][col] = ox # Add the checker to the spot above it
  37.                     return
  38.             self.data[self.height-1][col] = ox # Otherwise but the checker there
  39.  
  40.     def allowsMove(self, col): # Tests to see if a column has room for another move
  41.         if 0 <= col < self.width:
  42.             return self.data[0][col] == ' '
  43.         else:
  44.             return False
  45.  
  46.     def delMove(self, col): # Deletes the last move played in column passed to the function
  47.         for row in range(self.height): # For each row..
  48.             if self.data[row][col] != ' ': # If the indexed spot is not empty
  49.                 self.data[row][col] = ' ' # Override whatever is there with a space
  50.                 return
  51.         return
  52.  
  53.     def isFull(self): # Checks if the board is full or not
  54.         for values in self.data: # For each value in the entire board, if there's a space anywhere then it's not full (False). If there isn't a space, then the board is full (True).
  55.             if ' ' in values:
  56.                 return False
  57.         return True
  58.  
  59.     def winsFor(self, ox): # Checks to see if a checker ('X' or 'O') won the Connect 4.
  60.         for row in range(self.height): # Horizontal check
  61.             for col in range(self.width - 3): # Uses an anchor point (subtracting 3 from the width and so forth) from where to start checking so it doesn't go out of range.
  62.                 if self.data[row][col] == ox and self.data[row][col+1] == ox and self.data[row][col+2] == ox and self.data[row][col+3] == ox:
  63.                     return True
  64.         for row in range(self.height - 3): # Vertical check
  65.             for col in range(self.width):
  66.                 if self.data[row][col] == ox and self.data[row+1][col] == ox and self.data[row+2][col] == ox and self.data[row+3][col] == ox:
  67.                     return True
  68.         for row in range(self.height - 3): # SW>NE check
  69.             for col in range(self.width - 3):
  70.                 if self.data[row][col] == ox and self.data[row+1][col+1] == ox and self.data[row+2][col+2] == ox and self.data[row+3][col+3] == ox:
  71.                     return True
  72.         for row in range(3, self.height): # SE>NW check
  73.             for col in range(self.width - 3):
  74.                 if self.data[row][col] == ox and self.data[row-1][col+1] == ox and self.data[row-2][col+2] == ox and self.data[row-3][col+3] == ox:
  75.                     return True
  76.         return False
  77.  
  78.     def hostGame(self): # Makes the game more playable by asking the user their turn and checking for wins or ties automatically
  79.         move = 0 # This is used later so it will rotate back and forth between asking X to move and asking O to move
  80.         while self.isFull() == False and self.winsFor('OX') != True: # While the board is not full and nobody has won yet, continue the loop
  81.             if move == 0: # When move is 0, it's X's turn (X always goes first)
  82.                 print(game) # Prints the board in its current state, this variable is located at the end of the file
  83.                 X = int(input('Player "X", what column would you like to move? ')) # Asks for the user's input for which column to move, then turns it into an integer and stores it in X
  84.                 if self.allowsMove(X): # If that position is allowed...
  85.                     self.addMove(X, 'X') # Add that move for 'X'
  86.                     move += 1 # Increment move by 1 so that move won't be 0, and thus it will be O's turn
  87.                     if self.winsFor('X'): # Check to see if X won the game with that move
  88.                         print('Congratulations Player "X", you win!')
  89.                         return
  90.                 else:
  91.                     while self.allowsMove(X) == False: # While the user keeps putting invalid columns...
  92.                         X = int(input('Oops! Your response was invalid, try again: ')) # Keep asking until the user chooses a column that is allowed
  93.                     self.addMove(X, 'X')
  94.                     move += 1
  95.                     if self.winsFor('X'):
  96.                         print('Congratulations Player "X", you win!')
  97.                         return
  98.             else: # O's turn to play, same mechanics just for a different player
  99.                 print(game)
  100.                 O = int(input('Player "O", what column would you like to move? '))
  101.                 if self.allowsMove(O):
  102.                     self.addMove(O, 'O')
  103.                     move -= 1 # Decrements move so it will be 0 and thus will be X's turn again
  104.                     if self.winsFor('O'):
  105.                         print('Congratulations Player "O", you win!')
  106.                         return
  107.                 else:
  108.                     while self.allowsMove(O) == False:
  109.                         O = int(input('Oops! Your response was invalid, try again: '))
  110.                     move -= 1
  111.                     self.addMove(O, 'O')
  112.                     if self.winsFor('O'):
  113.                         print('Congratulations Player "O", you win!')
  114.                         return
  115.         print('It\'s a tie!') # If it breaks out of the loop and someone hasn't already won, then that mean the board must be full and thus would be a tie
  116.         return
  117.  
  118. def main():
  119.     game = Connect4(7, 6)
  120.     game.hostGame()
  121.     print(game)
  122.  
  123. if __name__=='__main__':
  124.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement