diff --git a/.gitignore b/.gitignore index c18dd8d..606ae18 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ __pycache__/ +env/ +test.py/ \ No newline at end of file diff --git a/connect4_env.py b/connect4_env.py deleted file mode 100644 index 20c678a..0000000 --- a/connect4_env.py +++ /dev/null @@ -1,74 +0,0 @@ -import numpy as np - -class Connect4Env: - ROWS = 6 - COLS = 7 - EMPTY = 0 - PLAYER1 = 1 - PLAYER2 = -1 - - def __init__(self): - self.board = np.zeros((self.ROWS, self.COLS), dtype=np.int8) - self.current_player = self.PLAYER1 - - def reset(self): - self.board[:] = 0 - self.current_player = self.PLAYER1 - return self.get_state() - - def get_state(self): - return self.board.copy(), self.current_player - - def available_actions(self): - return [col for col in range(self.COLS) if self.board[0, col] == 0] - - def step(self, action): - if action not in self.available_actions(): - raise ValueError("Invalid action") - - for row in reversed(range(self.ROWS)): - if self.board[row][action] == 0: - self.board[row][action] = self.current_player - break - - done, winner = self.check_win() - reward = 0 - if done: - if winner == 0: - reward = 0 # draw - elif winner == self.current_player: - reward = 1 - else: - reward = -1 - self.current_player *= -1 - return self.get_state(), reward, done - - def check_win(self): - for row in range(self.ROWS): - for col in range(self.COLS - 3): - line = self.board[row, col:col + 4] - if abs(sum(line)) == 4: - return True, np.sign(sum(line)) - - for row in range(self.ROWS - 3): - for col in range(self.COLS): - line = self.board[row:row + 4, col] - if abs(sum(line)) == 4: - return True, np.sign(sum(line)) - - for row in range(self.ROWS - 3): - for col in range(self.COLS - 3): - diag = [self.board[row + i][col + i] for i in range(4)] - if abs(sum(diag)) == 4: - return True, np.sign(sum(diag)) - - for row in range(3, self.ROWS): - for col in range(self.COLS - 3): - diag = [self.board[row - i][col + i] for i in range(4)] - if abs(sum(diag)) == 4: - return True, np.sign(sum(diag)) - - if all(self.board[0, :] != 0): - return True, 0 # draw - - return False, None diff --git a/main.py b/main.py index f90aa4b..1178237 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,5 @@ import os import sys -import socket from colours import Colours as C # =========================== @@ -77,11 +76,16 @@ def local_move_provider(player, board): col = getIntInput(f"{colourTile(player)} where do you want to drop your tile? 1-7.\n>>> ", board) - 1 return col -def socket_receive_move(sock): - return int(sock.recv(1024).decode()) - -def socket_send_move(sock, col): - sock.sendall(str(col).encode()) +def cpu_move_provider(player, board): + + i = 0 + while i < len(board): + if any([t == 'O' for t in board[i]]): + break + else: + i += 1 + print(i) + return i # =========================== # | Main game loop | @@ -136,8 +140,7 @@ def play_lan_client(): return def play_vs_computer(): - print("PvC mode coming soon!") - input("Press Enter to return to menu...") + play_game(cpu_move_provider, local_move_provider) # =========================== # | Menu |