cpu vs cpu mode, picking your colour against cpu.
This commit is contained in:
parent
2aec7528be
commit
cee3be9334
89
main.py
89
main.py
|
|
@ -1,5 +1,3 @@
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
from colours import Colours as C
|
from colours import Colours as C
|
||||||
import random
|
import random
|
||||||
import json
|
import json
|
||||||
|
|
@ -9,7 +7,7 @@ import json
|
||||||
# ===========================
|
# ===========================
|
||||||
|
|
||||||
def clear():
|
def clear():
|
||||||
os.system('cls' if sys.platform.startswith('win') else 'clear')
|
print(end='\033[2J\033[1;1H',flush=True)
|
||||||
|
|
||||||
def colourTile(tile):
|
def colourTile(tile):
|
||||||
try:
|
try:
|
||||||
|
|
@ -97,7 +95,7 @@ def getIntInput(prompt, board=None):
|
||||||
inp = input(prompt)
|
inp = input(prompt)
|
||||||
try:
|
try:
|
||||||
inp = int(inp)
|
inp = int(inp)
|
||||||
if not 1 <= inp <= 7:
|
if not 1 <= inp <= 8:
|
||||||
raise ValueError
|
raise ValueError
|
||||||
return inp
|
return inp
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
@ -148,12 +146,12 @@ def evalWindow(window, player):
|
||||||
if player_count == 4:
|
if player_count == 4:
|
||||||
score += 100
|
score += 100
|
||||||
elif player_count == 3 and empty_count == 1:
|
elif player_count == 3 and empty_count == 1:
|
||||||
score += 5
|
score += 50
|
||||||
elif player_count == 2 and empty_count == 2:
|
elif player_count == 2 and empty_count == 2:
|
||||||
score += 2
|
score += 2
|
||||||
|
|
||||||
if opponent_count == 3 and empty_count == 1:
|
if opponent_count == 3 and empty_count == 1:
|
||||||
score -= 4
|
score -= 100
|
||||||
|
|
||||||
return score
|
return score
|
||||||
|
|
||||||
|
|
@ -249,48 +247,6 @@ 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 cpu_move_provider(player, board):
|
|
||||||
# col = 0
|
|
||||||
# def other_player_gonna_win(my_move: int|None = None) -> int|bool:
|
|
||||||
# if my_move == None: my_move = col
|
|
||||||
# my_board = deepcopy(board)
|
|
||||||
# my_board[my_move][my_board[my_move].index('O')] = player
|
|
||||||
|
|
||||||
# other_p = 'Y' if player == 'R' else 'R'
|
|
||||||
# for other_p_col in range(len(my_board)):
|
|
||||||
# if 'O' not in my_board[other_p_col]:
|
|
||||||
# continue
|
|
||||||
|
|
||||||
# new_board = deepcopy(my_board)
|
|
||||||
# new_board[other_p_col][new_board[other_p_col].index('O')] = other_p
|
|
||||||
# if checkWin(new_board, other_p) != None:
|
|
||||||
# return other_p_col
|
|
||||||
# return False
|
|
||||||
|
|
||||||
# def im_gonna_win() -> int|bool:
|
|
||||||
# for possible_col in range(len(my_board)):
|
|
||||||
# if 'O' not in my_board[other_p_col]:
|
|
||||||
# continue
|
|
||||||
|
|
||||||
# new_board = deepcopy(my_board)
|
|
||||||
# new_board[other_p_col][new_board[other_p_col].index('O')] = other_p
|
|
||||||
# if checkWin(new_board, other_p) != None:
|
|
||||||
# return other_p_col
|
|
||||||
# return False
|
|
||||||
|
|
||||||
# # Start with a random move
|
|
||||||
# col = random.randint(0,6)
|
|
||||||
# while not any([t == 'O' for t in board[col]]):
|
|
||||||
# col += 1
|
|
||||||
# if col == 7: col = 0
|
|
||||||
|
|
||||||
# # Prevent other player winning 1 move deep
|
|
||||||
# if other_player_gonna_win():
|
|
||||||
# col = other_player_gonna_win()
|
|
||||||
|
|
||||||
# time.sleep((random.random()*0.5)+0.25) # Simulate thinking time
|
|
||||||
# return col
|
|
||||||
|
|
||||||
def cpu_move_provider(player, board):
|
def cpu_move_provider(player, board):
|
||||||
allowedMoves = [i for i, col in enumerate(board) if 'O' in col]
|
allowedMoves = [i for i, col in enumerate(board) if 'O' in col]
|
||||||
|
|
||||||
|
|
@ -374,8 +330,22 @@ def play_lan_client():
|
||||||
return
|
return
|
||||||
|
|
||||||
def play_vs_computer():
|
def play_vs_computer():
|
||||||
# play_game(cpu_move_provider, local_move_provider)
|
|
||||||
# play_game(local_move_provider, cpu_move_provider)
|
while True:
|
||||||
|
inp = input("Do you want to play as red or yellow? ")
|
||||||
|
try:
|
||||||
|
if not inp in ["r", "red", "y", "yellow"]:
|
||||||
|
raise ValueError
|
||||||
|
break
|
||||||
|
except ValueError:
|
||||||
|
print("Enter 'r', 'red', 'y' or 'yellow'.")
|
||||||
|
|
||||||
|
if inp in ["r", "red"]:
|
||||||
|
play_game(local_move_provider, cpu_move_provider)
|
||||||
|
elif inp in ["y", "yellow"]:
|
||||||
|
play_game(cpu_move_provider, local_move_provider)
|
||||||
|
|
||||||
|
def cpu_vs_cpu():
|
||||||
play_game(cpu_move_provider, cpu_move_provider)
|
play_game(cpu_move_provider, cpu_move_provider)
|
||||||
|
|
||||||
# ===========================
|
# ===========================
|
||||||
|
|
@ -391,13 +361,13 @@ def edit_settings():
|
||||||
}
|
}
|
||||||
|
|
||||||
# Load existing settings
|
# Load existing settings
|
||||||
if os.path.exists(settings_file):
|
try:
|
||||||
with open(settings_file, "r") as f:
|
with open(settings_file, "r") as f:
|
||||||
try:
|
try:
|
||||||
settings = json.load(f)
|
settings = json.load(f)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
settings = default_settings.copy()
|
settings = default_settings.copy()
|
||||||
else:
|
except:
|
||||||
settings = default_settings.copy()
|
settings = default_settings.copy()
|
||||||
|
|
||||||
# Keep a copy for detecting unsaved changes
|
# Keep a copy for detecting unsaved changes
|
||||||
|
|
@ -464,11 +434,12 @@ def edit_settings():
|
||||||
while True:
|
while True:
|
||||||
clear()
|
clear()
|
||||||
print("How do you want to play?")
|
print("How do you want to play?")
|
||||||
print("1. PvP (same device)")
|
print("1. Player vs Player (same device)")
|
||||||
print("2. PvP (LAN)")
|
print("2. Player vs Player (LAN)")
|
||||||
print("3. PvC (vs computer)")
|
print("3. Player vs CPU")
|
||||||
print("4. Edit settings")
|
print("4. CPU vs CPU")
|
||||||
print("5. Quit")
|
print("5. Edit settings")
|
||||||
|
print("6. Quit")
|
||||||
choice = input("Choose 1-4: ").strip()
|
choice = input("Choose 1-4: ").strip()
|
||||||
if choice == "1":
|
if choice == "1":
|
||||||
play_local_pvp()
|
play_local_pvp()
|
||||||
|
|
@ -480,8 +451,10 @@ while True:
|
||||||
elif choice == "3":
|
elif choice == "3":
|
||||||
play_vs_computer()
|
play_vs_computer()
|
||||||
elif choice == "4":
|
elif choice == "4":
|
||||||
edit_settings()
|
cpu_vs_cpu()
|
||||||
elif choice == "5":
|
elif choice == "5":
|
||||||
|
edit_settings()
|
||||||
|
elif choice == "6":
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
input("Invalid choice. Press Enter to try again...")
|
input("Invalid choice. Press Enter to try again...")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user