This repository has been archived by the owner on Jan 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise3.rb
176 lines (153 loc) · 3.44 KB
/
exercise3.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require_relative 'exercise2.rb'
# Exercise 3
def randomMove(board,symbol)
xy = getXY(rand(1..9))
while(!board[xy[0]][xy[1]].nil?)
xy = getXY(rand(1..9))
end
board[xy[0]][xy[1]] = symbol
end
class Value
attr_accessor :value
def initialize(v=nil)
@value = v
end
def cp(other)
@value = other.value
end
end
class Move
attr_accessor :x, :y
def initialize(x=nil,y=nil)
self.set(x,y)
end
def set(x,y)
@x, @y = x, y
end
def cp(other)
@x = other.x
@y = other.y
end
end
class Board
def show
for i in 0..2
for j in 0..2
if(@arr[i][j].nil?)
print(i*3+j+1)
else
print(@arr[i][j])
end
print " "
end
puts
end
end
end
# values: 4 - win, 3 - unknown, 2 - draw, 1 - loss
def miniMax(board,bestMove,bestValue,player1,player2,depth,maxDepth)
if(depth>maxDepth)
bestValue.value = 3
return
end
lowestValue = Float::INFINITY
lowestMove = Move.new
for i in 0..2
for j in 0..2
if(board[i][j].nil?)
board[i][j] = player1
if(board.win?(player1))
lowestValue = 1
lowestMove.set(i,j)
elsif(board.win?(player2))
lowestValue = 4
lowestMove.set(i,j)
elsif(board.full?)
lowestValue = 2
lowestMove.set(i,j)
else
testValue = Value.new
testMove = Move.new(i,j)
miniMax(board,testMove,testValue,player2,player1,depth+1,maxDepth)
if(testValue.value<lowestValue)
lowestValue = testValue.value
lowestMove.set(i,j)
end
end
board[i][j] = nil
end
end
end
bestMove.cp(lowestMove)
if(lowestValue == 4)
bestValue.value = 1
elsif(lowestValue == 1)
bestValue.value = 4
else
bestValue.value = lowestValue
end
end
def playTurn(symbol,player,board,level)
if(player==symbol)
print "Podaj pozycję #{symbol}(1-9): "
indx = gets.chomp.to_i
xy = getXY(indx)
while(!board[xy[0]][xy[1]].nil?)
print "Pozycja zajęta! Podaj prawidłową wartość: "
indx = gets.chomp.to_i
xy = getXY(indx)
end
place(indx,player,board)
else
puts "Ruch komputera #{symbol}"
if(level==0)
randomMove(board,symbol)
elsif(level==1)
mov, val = Move.new, Value.new
miniMax(board,mov,val,symbol,swp(symbol),0,3)
board[mov.x][mov.y] = symbol
elsif(level==2)
mov, val = Move.new, Value.new
miniMax(board,mov,val,symbol,swp(symbol),0,9)
board[mov.x][mov.y] = symbol
else
raise "Nieprawidłowy poziom trudności"
end
end
end
def playGame
board = Board.new
winner = nil
print "Wybierz poziom trudności(0, 1, 2): "
level = gets.chomp.to_i
while(level!=0&&level!=1&&level!=2)
print "Nieprawidłowy wybór. Możliwe opcje (0, 1, 2): "
level = gets.chomp.to_i
end
print "Wybierz X albo O: "
player = gets.chomp.upcase
while(player!="X"&&player!="O")
print "Nieprawidłowy wybór. Możliwe opcje (X,O): "
player = gets.chomp.upcase
end
loop do
playTurn("X",player,board,level)
board.show
if(board.win?("X"))
winner = "X"
break
elsif(board.full?)
break
end
playTurn("O",player,board,level)
board.show
if(board.win?("O"))
winner = "O"
break
elsif(board.full?)
break
end
end
puts winner=="X"||winner=="O" ? "Wygrywa gracz #{winner}!" : "Remis!"
end
# playGame