-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect4.py
40 lines (29 loc) · 1.1 KB
/
connect4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import latticode
ttt = latticode.Game('Connect 4')
brd = ttt.create_board(7, 6)
ttt.create_players('Blue', 'Red')
ttt.create_piece('Blue', sprite='Blue')
ttt.create_piece('Red', sprite='Red')
ttt.set_initial_state(latticode.EMPTY_BOARD)
ttt.add_sidelined_piece('Blue', count=latticode.INFINITY)
ttt.add_sidelined_piece('Red', count=latticode.INFINITY)
def legal_moves(piece, piece_loc, board):
if piece == board.current_player and piece_loc is None:
return latticode.to_moves(board.open_spaces_gravity())
return []
def make_move(piece, piece_loc, move, board):
new_board = board.copy()
new_board[move.loc] = piece
new_board.current_player = 'Blue' if new_board.current_player == 'Red' else 'Red'
return new_board
def check_status(game, board):
for player in game.players:
if board.in_line(4, player):
return player
if board.all_filled():
return latticode.TIE
return latticode.ONGOING
ttt.set_initial_player('Blue')
ttt.set_legal_moves_function(legal_moves)
ttt.set_make_move_function(make_move)
ttt.set_check_status_function(check_status)