Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
lengfeld8
1. Contest
Commits
ae149e9a
Commit
ae149e9a
authored
May 15, 2021
by
lengfeld8
Browse files
Upload New File
parent
b7df6e25
Changes
1
Hide whitespace changes
Inline
Side-by-side
main.py
0 → 100644
View file @
ae149e9a
# 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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment