-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.pi
92 lines (73 loc) · 2.23 KB
/
board.pi
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
num_cols=7.
num_rows=6.
black_Piece = 1.
red_Piece = 2.
% create default board
create_board_state() = Board =>
EmptyPos = [],
foreach (I in 1..num_rows())
EmptyPos := EmptyPos ++ [[0,0,0,0,0,0,0]]
end,
Board = create_board_state(EmptyPos, [0,0,0,0,0,0,0], 1).
% create board with given values
create_board_state(Position, ColCount, Turn) = new_map(3, ['pos' = Position, 'colCount' = ColCount, 'turn'= Turn]).
% create copy of board
copy_board_state(OldBoard) = NewBoard =>
NewBoard = create_board_state(),
NewBoardPos = NewBoard.get('pos'),
OldBoardPos = OldBoard.get('pos'),
foreach (Y in 1..num_rows())
foreach (X in 1..num_cols())
NewBoardPos[Y,X] := OldBoardPos[Y,X],
end
end,
NewBoard.put('turn', OldBoard.get('turn')),
NewBoardColCount = NewBoard.get('colCount'),
OldBoardColCount = OldBoard.get('colCount'),
foreach (I in 1..num_cols())
NewBoardColCount[I] := OldBoardColCount[I],
end.
% return legal moves given boardstate
legal_moves(Board) = LegalMoves =>
LegalMoves = [],
foreach(C in 1..num_cols())
Tmp := Board.get(colCount),
if Tmp[C] < num_rows() then
LegalMoves := LegalMoves ++ [C]
end
end.
% does Moves contain Move
contains(Moves, Move) = DoesContain =>
DoesContain = false,
foreach (M in Moves)
if M = Move then
DoesContain := true
end,
end.
% get opiset player piece
opposite (0) = 0.
opposite (Piece) = 3 - Piece.
% get X and Y val of move
move_Y_and_x_val(Board, Move) = Vals =>
Colval = Board.get('colCount'),
Vals = (num_rows()-Colval[Move], Move).
% create a new board with given move
make_move(Board, Move) = NewBoard =>
NewBoard = copy_board_state(Board),
NewColCount = NewBoard.get(colCount),
NewColCount[Move] := NewColCount[Move] + 1,
NewBoard.put('turn', opposite(turn(Board))),
NewPos = NewBoard.get('pos'),
MovePos = move_Y_and_x_val(Board, Move),
NewPos[MovePos[1], MovePos[2]] := Board.get('turn').
turn(Board) = Board.get(turn).
% print board state
print_board(Board) =>
BoardPos = Board.get(pos),
foreach (Y in 1..num_rows())
Row := BoardPos[Y],
foreach (X in 1..num_cols())
print(Row[X]),
end,
print("\n")
end.