-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkers.rb
116 lines (102 loc) · 3.01 KB
/
checkers.rb
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
@checkers_state = ["02020202",
"20202020",
"02020202",
"00000000",
"00000000",
"10101010",
"01010101",
"10101010"]
# start_board prints out the checker board in it's inital state
def start_board
puts ""
puts " 1 2 3 4 5 6 7 8"
puts " |-------------------------|"
puts " A || |\u25EF | |\u25EF | |\u25EF | |\u25EF ||"
puts " |-------------------------|"
puts " B ||\u25EF | |\u25EF | |\u25EF | |\u25EF | ||"
puts " |-------------------------|"
puts " C || |\u25EF | |\u25EF | |\u25EF | |\u25EF ||"
puts " |-------------------------|"
puts " D || | | | | | | | ||"
puts " |-------------------------|"
puts " E || | | | | | | | ||"
puts " |-------------------------|"
puts " F ||\u2B24 | |\u2B24 | |\u2B24 | |\u2B24 | ||"
puts " |-------------------------|"
puts " G || |\u2B24 | |\u2B24 | |\u2B24 | |\u2B24 ||"
puts " |-------------------------|"
puts " H ||\u2B24 | |\u2B24 | |\u2B24 | |\u2B24 | ||"
puts " |-------------------------|\n "
end
# introduce prints out a welcome and how to message
def introduce
puts "\n Welcome to REPL Checkers!"
puts " ---------------------------"
end
def legal_move?(row, column, to_row, to_column, player)
possible_moves = []
if (player == 1)
possible_moves << [row.to_i-1. column-1] if ((row
elsif (player == 2)
end
end
def move_piece(player, row, column)
if player == 1
print "Player \u2B24 choose a spot to move your piece (e.g. G2): "
elsif player == 2
print "Player \u25EF choose a spot to move your piece (e.g. B3): "
end
tile = gets.chomp.upcase
while (true) do
to_row = tile[0]
to_column = tile[1].to_i - 1
if (("A".."Z").include?(row) && (0..7).include?(column) && legal_move?(row, column, to_row, to_column, player)
puts "yay"
return
else
puts "\n *** Illegal tile chosen ***"
puts " Choose a proper tile: "
tile = gets.chomp.upcase
end
end
end
# is_piece_there? checks if there is a certain player's piece
# at the selected position
def is_piece_there?(row, column, player)
row_num = row.codepoints[0] - 65
@checkers_state[row_num][column].to_i == player ? true : false
end
# check_for_piece checks to see if the user entered a proper tile
def check_for_piece(tile, player)
while (true) do
row = tile[0]
column = tile[1].to_i - 1
if (("A".."Z").include?(row) && (0..7).include?(column) && is_piece_there?(row, column, player))
move_piece(player, row, column)
return
else
puts "\n *** Illegal tile chosen ***"
puts " Choose a proper tile: "
tile = gets.chomp.upcase
end
end
end
# pick_tile allows the user to pick a tile
def pick_tile(player)
if player == 1
print "Player \u2B24 choose a piece (e.g. G2): "
elsif player == 2
print "Player \u25EF choose a piece (e.g. B3): "
end
tile = gets.chomp.upcase
check_for_piece(tile, player)
end
def request_action
player = 1
pick_tile(player)
player = 2 if player == 1
player = 1 if player == 2
end
start_board
introduce
request_action