nraya 0.0.5 → 0.0.6
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.
- checksums.yaml +4 -4
- data/lib/main.rb +21 -20
- data/lib/player.rb +10 -16
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dc3e2dff8f3f3ee27bfa2839435adfba9291890f
|
|
4
|
+
data.tar.gz: 5d39881fe3b442dcb29ba24990dafa305b0eb541
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8ee706ba6b0ba7cdc41b1985bb4aa69edeb190fc124978a858dec7aa2d0b3d4a672281039e3d6c2dc7086b42fedd120e9540c10453e125b956c55342b0be7b0b
|
|
7
|
+
data.tar.gz: 06ac1c8fd719eabbd302bd419c339132202d62cf2cbddb2b47989dce18d90c73bc71e0cab736555597b4bca2c5c2bf744f43ea9b530c77cf375c35ad32ffc4e2
|
data/lib/main.rb
CHANGED
|
@@ -3,7 +3,7 @@ require 'player'
|
|
|
3
3
|
require 'nrayaex'
|
|
4
4
|
|
|
5
5
|
class Game
|
|
6
|
-
attr_reader :cols, :rows, :n, :x, :tokens
|
|
6
|
+
attr_reader :cols, :rows, :n, :x, :tokens, :winner
|
|
7
7
|
def initialize(n, players)
|
|
8
8
|
@cols = 2*n+1
|
|
9
9
|
@rows = 2*n-1
|
|
@@ -11,6 +11,7 @@ class Game
|
|
|
11
11
|
@players = players
|
|
12
12
|
@tokens = ((2*n-1)*(2*n-2)/players)
|
|
13
13
|
@playersArray = Array.new()
|
|
14
|
+
@winner = false
|
|
14
15
|
end
|
|
15
16
|
|
|
16
17
|
def initGame
|
|
@@ -36,25 +37,25 @@ class Game
|
|
|
36
37
|
end
|
|
37
38
|
|
|
38
39
|
def turns
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
40
|
+
while @winner == false do
|
|
41
|
+
player = @playersArray.shift
|
|
42
|
+
if player.tokens != 0
|
|
43
|
+
$stdout.print "> Enter the column that you like play "
|
|
44
|
+
$stdout.print "(#{player.name.chomp} :: #{player.sym} :: #{player.tokens}): "
|
|
45
|
+
$stdout.flush
|
|
46
|
+
x = gets.to_i
|
|
47
|
+
y = $board.setPoint(x, player.sym)
|
|
48
|
+
player.setPosition(x,y)
|
|
49
|
+
player.restToken()
|
|
50
|
+
@playersArray.push(player)
|
|
51
|
+
else
|
|
52
|
+
raise NoMoreTokens
|
|
53
|
+
end
|
|
54
|
+
if player.checkWin == true then
|
|
55
|
+
@winner = true
|
|
56
|
+
puts "Congrats #{player.name.chomp}! You won on nraya game!"
|
|
57
|
+
raise WeHaveWin
|
|
58
|
+
end
|
|
58
59
|
end
|
|
59
60
|
end
|
|
60
61
|
end
|
data/lib/player.rb
CHANGED
|
@@ -6,7 +6,7 @@ class Player
|
|
|
6
6
|
@name = name
|
|
7
7
|
@sym = sym
|
|
8
8
|
@tokens = tokens
|
|
9
|
-
@positions = Array.new()
|
|
9
|
+
@positions = Array.new($game.cols) {Array.new($game.cols, '.')}
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
public
|
|
@@ -15,34 +15,28 @@ class Player
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def setPosition(x,y)
|
|
18
|
-
|
|
19
|
-
@positions.push(point)
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def getPositions
|
|
23
|
-
return @positions
|
|
18
|
+
@positions[y][x-1] = @sym
|
|
24
19
|
end
|
|
25
20
|
|
|
26
21
|
def checkWin
|
|
27
22
|
#V: Vertical - #H: Horizontal - DR: Diagonal Right - DL: Diagonal Left
|
|
28
23
|
i, j, k, v, h, dr, dl = [0]*7
|
|
29
|
-
matriz =
|
|
30
|
-
|
|
24
|
+
matriz = @positions
|
|
25
|
+
matrizSize = matriz.size
|
|
26
|
+
until i >= matrizSize do
|
|
31
27
|
j = 0
|
|
32
|
-
until j >=
|
|
28
|
+
until j >= matrizSize do
|
|
33
29
|
if matriz[j][i] == @sym then v += 1 else v = 0 end
|
|
34
|
-
if matriz[i
|
|
35
|
-
if matriz[i][j] == @sym && i
|
|
30
|
+
if matriz[i][j] == @sym then h += 1 else h = 0 end
|
|
31
|
+
if matriz[i][j] == @sym && i < (matrizSize - $game.n + 1) then
|
|
36
32
|
k = 0
|
|
37
|
-
until k >= $game.n
|
|
33
|
+
until k >= $game.n do
|
|
38
34
|
if matriz[i+k][j+k] == @sym then dl += 1 else dl = 0 end
|
|
39
35
|
if matriz[i+k][j-k] == @sym then dr += 1 else dr = 0 end
|
|
40
36
|
k += 1
|
|
41
37
|
end
|
|
42
38
|
end
|
|
43
|
-
if ( v == $game.n || h == $game.n || dr == $game.n || dl == $game.n ) then
|
|
44
|
-
return true
|
|
45
|
-
end
|
|
39
|
+
if ( v == $game.n || h == $game.n || dr == $game.n || dl == $game.n ) then return true end
|
|
46
40
|
j += 1
|
|
47
41
|
end
|
|
48
42
|
i += 1
|