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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/main.rb +21 -20
  3. data/lib/player.rb +10 -16
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c1802e4a9d9d4f368836a482f9dae706aae585c
4
- data.tar.gz: 55b51135ed17a5d74c44297e4f5fc06f02ced012
3
+ metadata.gz: dc3e2dff8f3f3ee27bfa2839435adfba9291890f
4
+ data.tar.gz: 5d39881fe3b442dcb29ba24990dafa305b0eb541
5
5
  SHA512:
6
- metadata.gz: 3c636c411ab031a54c4d94eaba75acb902652fbd43bc88cf9d4d4ffe7e194f187565cdb54dca58dd1a03a1bcda6beee10e6d6cdaeac1597b585ae408b9a4d976
7
- data.tar.gz: 324d2f24acaf843985f7194b352692c04482aa50781472c662ac8e2688581d6ffdbc3b894b8ae82aac5c033bc07fd390862920e8d589b0df8a23fbf712610845
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
- player = @playersArray.shift
40
- if player.tokens != 0
41
- $stdout.print "> Enter the column that you like play "
42
- $stdout.print "(#{player.name.chomp} :: #{player.sym} :: #{player.tokens}): "
43
- $stdout.flush
44
- x = gets.to_i
45
- y = $board.setPoint(x, player.sym)
46
- player.setPosition(x,y)
47
- player.getPositions()
48
- player.restToken()
49
- @playersArray.push(player)
50
- else
51
- raise NoMoreTokens
52
- end
53
- if player.checkWin == true then
54
- puts "Congrats #{player.name.chomp}! You won on nraya game!"
55
- raise WeHaveWin
56
- else
57
- turns()
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
- point = [x,y]
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 = $board.getMatriz
30
- until i >= $game.rows do
24
+ matriz = @positions
25
+ matrizSize = matriz.size
26
+ until i >= matrizSize do
31
27
  j = 0
32
- until j >= $game.cols do
28
+ until j >= matrizSize do
33
29
  if matriz[j][i] == @sym then v += 1 else v = 0 end
34
- if matriz[i+2][j] == @sym then h += 1 else h = 0 end
35
- if matriz[i][j] == @sym && i > ($game.rows - $game.n + 1) then
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nraya
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josue Daniel Bustamante - Santiago Baena