Commit ae149e9a authored by lengfeld8's avatar lengfeld8
Browse files

Upload New File

parent b7df6e25
# main.py manages the two-way communication with the game-server, which means parsing json to data and vice versa
import numpy as np
import time
import sys
import json
from Board import Board
from alphaBeta import AlphaBeta
def main(size_of_board):
actualBoard = np.zeros(size_of_board * size_of_board)
start_main = Board(size_of_board, actualBoard)
# start game
# moves are randomly chosen
setPlayer = 1
gameRound = 1
gameMoves = start_main.generate_moves()
print("Starting Game, there are: {} ".format(len(gameMoves)) + "possible moves. \n" + "Server game delay set to 2s")
time.sleep(5)
while len(gameMoves) > 0:
print("Player {} in ".format(setPlayer) + "Round {} :\n".format(gameRound))
start_main.choose_rand_move(gameMoves)
gameMoves = start_main.generate_moves()
start_main.print_board()
print("Remaining possible moves for this board state: {} \n".format(len(gameMoves)))
if setPlayer == 1:
setPlayer = 2
else:
setPlayer = 1
gameRound += 1
time.sleep(2)
print("Game over after {} rounds. \n".format(gameRound - 1))
print("The final score is {}".format(start_main.eval_func(start_main.board)))
start_main.print_final_board()
string1 = "bw--bww/wbbwwbb/bwbwbbw/-bwwbbw/bw-b-w-/w--w-b-/-wbbw--" # 14 possible moves
string1_9x9 = "w--w--w-w/b--b--b-b/-----w---/-----b---/-wb-w--wb/----b----/" \
"---------/w-wb-w--w/b----b--b" # 152 possible moves
string2_9x9 = "bw-wbwbw-/-b-b-bwbb/-w---w-ww/-bwwbb-bw/bb---bw--/wwbw--bbw/" \
"bb-b--ww-/wwwwbw-b-/--b--bw--" # 42 possible moves
string2 = "b---bww/wbbw--b/-wbw-bw/--b--bw/bwwb-w-/---w-b-/-wbbw--" # 32 possible moves
if __name__ == '__main__':
message = ''
for line in sys.stdin:
message += line.strip()
if message[0] != '{': message = '{'
if 'currentTurn' in message: message += '}'
if '}' in message:
try:
if 'start' in message:
message = ''
elif 'running' in message:
json_data = json.loads(message)
#your move here:
start_alphaBeta = AlphaBeta(json_data['boardSize'])
start_alphaBeta.boardObject.string_parse(str(json_data['currentBoard']))
available_moves = start_alphaBeta.boardObject.generate_moves()
if json_data['currentTurn'] == 1:
x = json_data['boardSize'] // 2
y = x + json_data['boardSize']
print(f"{y},{x}", end='')
else:
best_move = start_alphaBeta.start_pruning(json_data['boardSize'], json_data['yourColour'], available_moves)
x = int(best_move[0])
y = int(best_move[1])
print(f"{x},{y}", end='')
sys.stdout.flush()
message = ''
except ValueError as err:
continue
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment