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__/
|
__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
|
|
||||||
17
main.py
17
main.py
|
|
@ -1,6 +1,5 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import socket
|
|
||||||
from colours import Colours as C
|
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
|
col = getIntInput(f"{colourTile(player)} where do you want to drop your tile? 1-7.\n>>> ", board) - 1
|
||||||
return col
|
return col
|
||||||
|
|
||||||
def socket_receive_move(sock):
|
def cpu_move_provider(player, board):
|
||||||
return int(sock.recv(1024).decode())
|
|
||||||
|
|
||||||
def socket_send_move(sock, col):
|
i = 0
|
||||||
sock.sendall(str(col).encode())
|
while i < len(board):
|
||||||
|
if any([t == 'O' for t in board[i]]):
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
print(i)
|
||||||
|
return i
|
||||||
|
|
||||||
# ===========================
|
# ===========================
|
||||||
# | Main game loop |
|
# | Main game loop |
|
||||||
|
|
@ -136,8 +140,7 @@ def play_lan_client():
|
||||||
return
|
return
|
||||||
|
|
||||||
def play_vs_computer():
|
def play_vs_computer():
|
||||||
print("PvC mode coming soon!")
|
play_game(cpu_move_provider, local_move_provider)
|
||||||
input("Press Enter to return to menu...")
|
|
||||||
|
|
||||||
# ===========================
|
# ===========================
|
||||||
# | Menu |
|
# | Menu |
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user