tic_tac_toe_pernix 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/tictactoe.rb +155 -0
- metadata +65 -0
data/lib/tictactoe.rb
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
class Tictactoe
|
2
|
+
|
3
|
+
attr_accessor :board
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@board = [ [nil, nil, nil],
|
7
|
+
[nil, nil, nil],
|
8
|
+
[nil, nil, nil]]
|
9
|
+
end
|
10
|
+
|
11
|
+
def playerMoves (row,column,symbol)
|
12
|
+
if(isSquareEmpty(row, column)) then
|
13
|
+
@board[row][column] = symbol
|
14
|
+
return true
|
15
|
+
end
|
16
|
+
return false
|
17
|
+
end
|
18
|
+
|
19
|
+
def printboard
|
20
|
+
p board
|
21
|
+
end
|
22
|
+
|
23
|
+
def isBoardEmpty
|
24
|
+
for fila in 0 ... 3
|
25
|
+
for column in 0 ... 3
|
26
|
+
return false if !board[fila][column].nil?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
def isBoardFull
|
32
|
+
for row in 0 ... 3
|
33
|
+
for column in 0 ... 3
|
34
|
+
if @board[row][column].nil?
|
35
|
+
return false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
return true
|
40
|
+
end
|
41
|
+
def isSquareEmpty(row, column)
|
42
|
+
@board[row][column].nil?
|
43
|
+
end
|
44
|
+
|
45
|
+
def restartGame
|
46
|
+
for row in 0 ... 3
|
47
|
+
for column in 0 ... 3
|
48
|
+
@board[row][column] = nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def IAMoves(symbol)
|
54
|
+
if isBoardFull() then
|
55
|
+
return false
|
56
|
+
else
|
57
|
+
for row in 0 ... 3
|
58
|
+
for column in 0 ... 3
|
59
|
+
#WIN: the IA has posibilities of win the match
|
60
|
+
p "IA searching"
|
61
|
+
if(playerMoves(row, column, symbol)) then
|
62
|
+
if(isAWinner(row,column, symbol)) then
|
63
|
+
p "<<IA wins>> Fila: " <<row << "Columna: " << column
|
64
|
+
return true
|
65
|
+
else
|
66
|
+
p "IA BORRA a ganar>> " <<row << ", "<< column
|
67
|
+
@board[row][column] = nil
|
68
|
+
end
|
69
|
+
#Block: the player has posibilities to win the match
|
70
|
+
_humanSymbol = (symbol == "X"? "O" : "X")
|
71
|
+
playerMoves(row, column, _humanSymbol)
|
72
|
+
if(isAWinner(row, column, _humanSymbol)) then
|
73
|
+
@board[row][column]= nil
|
74
|
+
playerMoves(row, column, symbol)
|
75
|
+
p "<<IA Blocks>>"
|
76
|
+
return true
|
77
|
+
else
|
78
|
+
p "IA borra a bloquear"
|
79
|
+
@board[row][column] = nil
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
#Fork: the player has two chances of win the match
|
85
|
+
#Block fork: the oponent has two chaces of win the match
|
86
|
+
#Center: must be the first move on a empty game
|
87
|
+
#Empty corner: the player plays a corner of the square
|
88
|
+
#Empty side: the player plays in a middle square on any side
|
89
|
+
if(isSquareEmpty(1,1)) then
|
90
|
+
playerMoves(1,1, symbol)
|
91
|
+
else
|
92
|
+
for row in 0 ... 3
|
93
|
+
for column in 0 ... 3
|
94
|
+
p "IA searching..."
|
95
|
+
if(playerMoves(row,column, symbol)) then
|
96
|
+
return true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
return false
|
103
|
+
end
|
104
|
+
|
105
|
+
def isAWinner(row, column, symbol)
|
106
|
+
if winnerInRow(row, symbol) then
|
107
|
+
p "<<Fila gana>>"
|
108
|
+
return true
|
109
|
+
elsif winnerInColumn(column, symbol) then
|
110
|
+
p "<<Columna gana>>"
|
111
|
+
return true
|
112
|
+
elsif winnerInDiag(symbol) then
|
113
|
+
p "<<Diag gana>>"
|
114
|
+
return true
|
115
|
+
elsif antiDiagWins(symbol) then
|
116
|
+
p "<<Antidiag gana>>"
|
117
|
+
return true
|
118
|
+
end
|
119
|
+
return false
|
120
|
+
end
|
121
|
+
|
122
|
+
def winnerInRow(row, symbol)
|
123
|
+
for column in 0 ... 3
|
124
|
+
if @board[row][column].nil? || @board[row][column] != symbol
|
125
|
+
return false
|
126
|
+
end
|
127
|
+
end
|
128
|
+
return true
|
129
|
+
end
|
130
|
+
|
131
|
+
def winnerInColumn(column, symbol)
|
132
|
+
for row in 0 ... 3
|
133
|
+
if @board[row][column].nil? || @board[row][column] != symbol
|
134
|
+
return false
|
135
|
+
end
|
136
|
+
end
|
137
|
+
return true
|
138
|
+
end
|
139
|
+
|
140
|
+
def winnerInDiag(symbol)
|
141
|
+
for row in 0 ... 3
|
142
|
+
for column in 0 ... 3
|
143
|
+
if(row == column) then
|
144
|
+
if @board[row][column].nil? || @board[row][column] != symbol
|
145
|
+
return false
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
return true
|
151
|
+
end
|
152
|
+
def antiDiagWins(symbol)
|
153
|
+
return (@board[0][2] == symbol && @board[1][1] ==symbol && @board[2][0] == symbol)
|
154
|
+
end
|
155
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tic_tac_toe_pernix
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 17
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 1.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Esteban Hernandez
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-01-15 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: A simple tictactoe game
|
22
|
+
email: jehehe1@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/tictactoe.rb
|
31
|
+
homepage: http://rubygems.org/gems/tictactoe_ehdez
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.24
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Tictactoe game!
|
64
|
+
test_files: []
|
65
|
+
|