i made a whole neural network but it was depressingly bad. imma try minmax with alphabeta pruning for look ahead
This commit is contained in:
parent
760c412a45
commit
9a47d3fd85
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1 +1,3 @@
|
|||
__pycache__/
|
||||
env/
|
||||
test.py/
|
||||
|
|
@ -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
|
||||
19
main.py
19
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 |
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user