Skip to content
Newer
Older
100644 125 lines (104 sloc) 3.51 KB
f2eaa2a add game, human and machine players, and illustrator. machine player …
Kevin Liddle authored Dec 7, 2011
1 use("lib/board")
61f19c0 pull prints out of game
Kevin Liddle authored Dec 27, 2011
2 use("lib/rules")
f2eaa2a add game, human and machine players, and illustrator. machine player …
Kevin Liddle authored Dec 7, 2011
3
4 MachinePlayer = Origin mimic
5
6 MachinePlayer initialize = method(marker_value,
7 self marker_value = marker_value
8 )
9
10 MachinePlayer get_move = method(board,
8a50f2f speed up game
Kevin Liddle authored Dec 17, 2011
11 if(board blank? or other_player_took_middle(board),
12 board corners random,
13 move = block_or_win(board)
14 if(move == [],
15 move = if(first_move_as_player_2?(board),
16 [board middle_index, board middle_index],
17 minimax(board, marker_value, 1) keys first
18 )
19 )
20 move
f2eaa2a add game, human and machine players, and illustrator. machine player …
Kevin Liddle authored Dec 7, 2011
21 )
22 )
90a0494 minimax working, refactor some things, catch conditions on user input
Kevin Liddle authored Dec 7, 2011
23
f2eaa2a add game, human and machine players, and illustrator. machine player …
Kevin Liddle authored Dec 7, 2011
24 MachinePlayer minimax = method(board, turn, depth,
7d83ae9 refactorings and separate game runner
Kevin Liddle authored Dec 7, 2011
25
f2eaa2a add game, human and machine players, and illustrator. machine player …
Kevin Liddle authored Dec 7, 2011
26 best_move = {}
8a50f2f speed up game
Kevin Liddle authored Dec 17, 2011
27 winning_move = game_saving_move(board, turn)
28
29 if(winning_move != [],
30 best_move = {winning_move => inverse(turn * depth)}
31 )
7d83ae9 refactorings and separate game runner
Kevin Liddle authored Dec 8, 2011
32
8a50f2f speed up game
Kevin Liddle authored Dec 17, 2011
33 if(best_move == {},
34 board dimension times(row,
35 board dimension times(column,
36
37 if(board empty_at?(row, column),
38
39 board set_space(row, column, turn)
40 if(Rules game_over?(board),
41 if( better_move_than_best_move?(move_score(board, depth + 1), best_move, turn),
42 best_move = { [row, column] => move_score(board, depth + 1 ) }
43 ),
44 ;else
45 next_level_best_move = minimax(board, turn negation, depth + 1)
46 next_move = { [row, column] => get_score(next_level_best_move) }
47 if( better_move_than_best_move?( get_score(next_move), best_move, turn ),
48 best_move = next_move
49 )
f2eaa2a add game, human and machine players, and illustrator. machine player …
Kevin Liddle authored Dec 7, 2011
50 )
7d83ae9 refactorings and separate game runner
Kevin Liddle authored Dec 8, 2011
51
8a50f2f speed up game
Kevin Liddle authored Dec 17, 2011
52 board reset_space(row, column)
53 )
f2eaa2a add game, human and machine players, and illustrator. machine player …
Kevin Liddle authored Dec 7, 2011
54 )
8a50f2f speed up game
Kevin Liddle authored Dec 17, 2011
55 ) ;end of iteration
56 )
57 best_move
58 )
59
60 MachinePlayer first_move_as_player_2? = method(board,
61 board number_of_moves == 1
62 )
63
64 MachinePlayer other_player_took_middle = method(board,
65 board number_of_moves == 1 and board empty_at?(board middle_index, board middle_index) not
66 )
67
68 MachinePlayer block_or_win = method(board,
69 best_move = immediate_winning_move(board)
70 if(best_move == [],
71 best_move = immediate_blocking_move(board)
72 )
f2eaa2a add game, human and machine players, and illustrator. machine player …
Kevin Liddle authored Dec 7, 2011
73 best_move
74 )
75
8a50f2f speed up game
Kevin Liddle authored Dec 17, 2011
76 MachinePlayer game_saving_move = method(board, turn,
77 saving_move = []
78 upcoming_winner = turn * (board dimension - 1)
79 board dimension times(group_index,
80 if(board get_row(group_index) reduce(+) == upcoming_winner,
81 saving_move = [group_index, board get_row(group_index) index(0)]
82 )
83 if(board get_column(group_index) reduce(+) == upcoming_winner,
84 saving_move = [board get_column(group_index) index(0), group_index]
85 )
86 )
87 if(board get_back_diagonal reduce(+) == upcoming_winner,
88 saving_move = [board get_back_diagonal index(0), board get_back_diagonal index(0)]
89 )
90 if(board get_forward_diagonal reduce(+) == upcoming_winner,
91 saving_move = [board dimension - board get_forward_diagonal index(0) - 1, board get_forward_diagonal index(0)]
92 )
93 saving_move
94 )
95
96 MachinePlayer immediate_winning_move = method(board,
97 game_saving_move(board, marker_value)
98 )
99
100 MachinePlayer immediate_blocking_move = method(board,
101 game_saving_move(board, marker_value * -1)
102 )
103
7d83ae9 refactorings and separate game runner
Kevin Liddle authored Dec 8, 2011
104 MachinePlayer move_score = method(board, depth,
105 inverse(Rules winner(board) * depth)
106 )
107
108 MachinePlayer better_move_than_best_move? = method(score, best_move, turn,
90a0494 minimax working, refactor some things, catch conditions on user input
Kevin Liddle authored Dec 7, 2011
109 best_move == {} or if(turn > 0,
110 score > get_score(best_move),
111 score < get_score(best_move)
f2eaa2a add game, human and machine players, and illustrator. machine player …
Kevin Liddle authored Dec 7, 2011
112 )
113 )
114
115 MachinePlayer inverse = method(value,
116 if(value != 0,
117 1 / value,
118 0
119 )
120 )
90a0494 minimax working, refactor some things, catch conditions on user input
Kevin Liddle authored Dec 7, 2011
121
122 MachinePlayer get_score = method(move,
123 move[move keys first]
124 )
Something went wrong with that request. Please try again.