-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.rb
121 lines (113 loc) · 3.57 KB
/
draw.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
115
116
117
118
119
120
121
require "rainbow"
using Rainbow
# Beskrivning: Omvandlar en position på spelbrädan till rutans betäckning
# Argument 1: Vector2 - positionen som ska omvandlas
# Return:
# String - Rutans betäckning
# nil - ogiltig inmatning
# Exempel:
# position_to_square_name(Vector2.new(0, 0)) => a1
# position_to_square_name(Vector2.new(7, 7)) => h8
# position_to_square_name(Vector2.new(3, 3)) => d4
# position_to_square_name(Vector2.new(5, 7)) => f8
# position_to_square_name(Vector2.new(-1, 4)) => nil
# position_to_square_name(Vector2.new(3, 15)) => nil
# Datum: 5/5/2024
# Namn: Noah Westerberg
def position_to_square_name(position)
column = ""
if position.x == 0
column += "a"
elsif position.x == 1
column += "b"
elsif position.x == 2
column += "c"
elsif position.x == 3
column += "d"
elsif position.x == 4
column += "e"
elsif position.x == 5
column += "f"
elsif position.x == 6
column += "g"
elsif position.x == 7
column += "h"
else
return nil
end
if position.y >= 0 && position.y < 8
return "#{column}#{position.y + 1}"
end
end
# Beskrivning: Ritar ut spelbrädan
# Argument 1: Array - spelbrädan
# Argument 2: Boolean - om brädan ska vara omvänd
# Argument 3: Array - positioner på rutor som ska vara markerade
# Datum: 6/5/2024
# Namn: Noah Westerberg
def draw_board(board, fliped, highlighted_squares)
if fliped
print " H|G|F|E|D|C|B|A\n"
i = 0
loop_end_value = board.length
else
print " A|B|C|D|E|F|G|H\n"
i = board.length - 1
loop_end_value = -1
end
# looparna går fram-/baklänges beroende på om brädan ska vara vänd eller inte
while i != loop_end_value
print "#{i+1}|"
j = 0
if fliped
j = board.length - 1
end
while j != 7 - loop_end_value
current_square = board[i][j]
square_text = current_square.icon
if board[i][j].class != Knight
square_text += " "
end
if board[i][j].class == Empty || board[i][j].class == En_passant_square
square_text = Rainbow(square_text).hide
elsif board[i][j].color == "white"
square_text = Rainbow(square_text).lightblue
else
square_text = Rainbow(square_text).black
end
if (j + i) % 2 == 0
square_text = Rainbow(square_text).bg(:brown)
else
square_text = Rainbow(square_text).bg(:burlywood)
end
if highlighted_squares.include?(Vector2.new(j, i))
if board[i][j].class == Empty || board[i][j].class == En_passant_square
square_text = position_to_square_name(Vector2.new(j, i))
square_text = Rainbow(square_text).bg(:blue).bisque
if board[i][j].class == En_passant_square
square_text = Rainbow(square_text).red
end
else
square_text = Rainbow(square_text).bg(:blue).red
end
end
print square_text
if fliped
j -= 1
else
j += 1
end
end
print "|#{i+1}\n"
if fliped
i += 1
else
i -= 1
end
end
if fliped
print " H|G|F|E|D|C|B|A\n"
else
print " A|B|C|D|E|F|G|H\n"
end
end