ttt_chido 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4f17cc0bb572d6a2258cfdbf6c5ea993b8bb137a
4
+ data.tar.gz: 572ebfb5821391670e53f554dadbce3063c5c91c
5
+ SHA512:
6
+ metadata.gz: bf4a63dee03e78f5235280172a4469380585eb76d4bacd7c5f3fc5449e817514085fe2c66d8d990a396c58c8bbe2cbb27c63dd0ea755aff0061e58fc1737717f
7
+ data.tar.gz: bbd948fb69897cf7c5031baee2b0e5f3f39dafca6586b5342740dde92c94955461cd9a824f551eea46714957ed0caf3e40752528ac8190cb07d6f6cb7f781f3a
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ttt_chido"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/game.rb ADDED
@@ -0,0 +1,155 @@
1
+ require 'winner'
2
+ require "colorize"
3
+
4
+ class Board
5
+ #To valid and set the size of the board n*n
6
+ def initialize(player1Start)
7
+ check = false
8
+ while check == false do
9
+ print "Size of de board nxn: "
10
+ size = gets.chomp.to_i
11
+ if( size != nil and size >0)
12
+ check = true
13
+ else
14
+ print 'Invalid number. '
15
+ gets.chomp
16
+ system('clear')
17
+ end
18
+ end
19
+ controlFlow(size,player1Start)
20
+ end
21
+
22
+ #To have the flow of all the game and validations
23
+ #To control the turn of the player
24
+ #To display and call the methods that changes the board
25
+ #To call the methods to check if a player won
26
+ private def controlFlow(size,player1Start)
27
+ @@sizeBoard = size
28
+ winner = Winner.new(@@sizeBoard)
29
+ @player = player1Start
30
+ board = cleanBoard()
31
+ 3.times {puts "\n"}
32
+
33
+ loop do
34
+ #To the turn of player X
35
+
36
+ case @player
37
+ when 1
38
+ @player = 1
39
+ displayBoard(board)
40
+ move = playerTurn(board,"Player X : ")
41
+ board = changeBoard(move,1,board)
42
+ setLastPlayer(@player) if true == winner.countingSymbol(board,"X") or nil == winner.countingSymbol(board,"X")
43
+ break if true == winner.countingSymbol(board,"X")
44
+ break if nil == winner.countingSymbol(board,"X")
45
+ @player = 2
46
+ when 2
47
+ #To the turn of player O
48
+ @player = 2
49
+ displayBoard(board)
50
+ move = playerTurn(board,"Player O :")
51
+ board = changeBoard(move,2,board)
52
+ system('clear')
53
+ setLastPlayer(@player) if true == winner.countingSymbol(board,"O") or nil == winner.countingSymbol(board,"O")
54
+ break if true == winner.countingSymbol(board,"O")
55
+ break if nil == winner.countingSymbol(board,"X")
56
+ @player = 1
57
+ end
58
+ 3.times {puts "\n"}
59
+ end
60
+ displayBoard(board)
61
+ end
62
+
63
+ #To display in the console which player it's gonna play
64
+ private def playerTurn(board,player)
65
+ check = false
66
+ while check == false do
67
+ puts ""
68
+ puts "..........................."
69
+ print "\t"+player
70
+ aux = gets.chomp
71
+ aux = aux.to_i
72
+ if( aux.is_a?(Integer) and aux > 0)
73
+ check = true if goodMovement(board,aux)
74
+ end
75
+ puts "\t\tInvalid position" if check == false
76
+ end
77
+ return aux
78
+ end
79
+
80
+ #To check if the movement of the player is valid
81
+ private def goodMovement(board,move)
82
+ move = move.to_s
83
+ board.each do |block|
84
+ if block.include?(move)
85
+ return true
86
+ end
87
+ end
88
+ return false
89
+ end
90
+
91
+ #To do the changes of the movement of the player
92
+ private def changeBoard(move,player,board)
93
+ move = move.to_s
94
+ board.each_with_index do |block,index|
95
+ if( block.include?(move) )
96
+ case player
97
+ when 1
98
+ board[index] = block.gsub(/\t#{move}\t/,"\tX\t".colorize(:blue))
99
+ when 2
100
+ board[index] = block.gsub(/\t#{move}\t/,"\tO\t".colorize(:red))
101
+ end
102
+ end
103
+ end
104
+ return board
105
+ end
106
+
107
+ #To build a new Board
108
+ private def cleanBoard()
109
+ index = 1
110
+ j=1
111
+ board = []
112
+ @@lineBoard = ""
113
+ while j <= @@sizeBoard do
114
+ i=1
115
+ line = ""
116
+ while i <= @@sizeBoard do
117
+ if (i == @@sizeBoard)
118
+ board << "\t#{index}\t"
119
+ line+="________________"
120
+ else
121
+ board <<"\t#{index}\t|"
122
+ line+="________________"
123
+ end
124
+ i+=1
125
+ index+=1
126
+ end
127
+ @@lineBoard = line if @@lineBoard != line
128
+ j+=1
129
+ end
130
+ return board
131
+ end
132
+
133
+ #To display the board any time
134
+ private def displayBoard(board)
135
+ index = 1
136
+ board.each_with_index do |block,position|
137
+ print block
138
+ if index == @@sizeBoard
139
+ puts ""
140
+ index = 0
141
+ puts @@lineBoard if position < board.length-1
142
+ end
143
+ index+=1
144
+ end
145
+ return
146
+ end
147
+
148
+ private def setLastPlayer(player)
149
+ @@lastWinner = player
150
+ end
151
+
152
+ public def getLastPlayer
153
+ return @@lastWinner
154
+ end
155
+ end
@@ -0,0 +1,3 @@
1
+ module TttChido
2
+ VERSION = "0.1.1"
3
+ end
data/lib/ttt_chido.rb ADDED
@@ -0,0 +1,40 @@
1
+ require "ttt_chido/version"
2
+ require 'game'
3
+ require "colorize"
4
+
5
+ module TttChido
6
+ class TicTacToe
7
+ def initialize;end
8
+
9
+ public def start
10
+ @salir = true;
11
+ @player = 1
12
+ while @salir do
13
+ system('clear')
14
+ puts '========================= M E N U ================================='
15
+ print ' Press enter to start '
16
+ gets.chomp
17
+ system('clear')
18
+ start = Board.new(@player)
19
+ @player = start.getLastPlayer
20
+ case @player
21
+ when 1
22
+ @player+=1
23
+ when 2
24
+ @player-=1
25
+ end
26
+ puts ""
27
+ print "Press enter to continue: "
28
+ gets.chomp
29
+ system('clear')
30
+ puts 'Want to play again? true/false'
31
+ answer = gets.chomp
32
+ @salir = false if answer == 'false'
33
+ system('clear')
34
+ end
35
+ puts "~~~~~~~~~~~~~~~~ Thanks for playing ~~~~~~~~~~~~~~~~~"
36
+ puts "~~~~~~~~~~~~~ Gem created by rubiocanino ~~~~~~~~~~~~~"
37
+ puts ""
38
+ end
39
+ end
40
+ end
data/lib/winner.rb ADDED
@@ -0,0 +1,146 @@
1
+ require "colorize"
2
+
3
+ class Winner
4
+ #To assign the size of the board
5
+ def initialize(size)
6
+ @@sizeBoard = size
7
+ end
8
+
9
+ #To control and check the ways to win the game
10
+ public def countingSymbol(board,symbol)
11
+ return nil if checkTie(board)
12
+ return true if checkHorizontal(board,symbol)
13
+ return true if checkVertical(board,symbol)
14
+ return true if checkDiagonal(board,symbol)
15
+ return false
16
+ end
17
+
18
+ #To check the Horizonal ways to win the game
19
+ private def checkHorizontal(board,symbol)
20
+ playerWon = false
21
+ index = 0
22
+ findSymbolArray = []
23
+ board.each do |block|
24
+ findSymbolArray << block.include?(symbol)
25
+ break if playerWon
26
+ if findSymbolArray.length == @@sizeBoard
27
+ playerWon = true if findSymbolArray.all? {|findValue| findValue == true}
28
+ index = 0
29
+ findSymbolArray = []
30
+ end
31
+ index+=1
32
+ end
33
+ congratulationPlayer(board,symbol) if playerWon
34
+ return playerWon
35
+ end
36
+
37
+ #To check the Vertical ways to win the game
38
+ private def checkVertical(board,symbol)
39
+ playerWon = false
40
+ findSymbolArray = []
41
+ board.each_with_index do |block,index|
42
+ jump = index
43
+ count = 1
44
+ if block.include?(symbol)
45
+ loop do
46
+ findSymbolArray << board[jump].include?(symbol) if count <= @@sizeBoard and jump < board.length
47
+ if count == @@sizeBoard
48
+ playerWon = findSymbolArray.all? {|findValue| findValue == true}
49
+ break
50
+ end
51
+ count+=1
52
+ jump+=@@sizeBoard
53
+ break if jump >= board.length
54
+ end
55
+ findSymbolArray = []
56
+ break if playerWon
57
+ end
58
+ end
59
+ congratulationPlayer(board,symbol) if playerWon
60
+ return playerWon
61
+ end
62
+
63
+ #To check the Diagonal way to win the game
64
+ private def checkDiagonal(board,symbol)
65
+ playerWon = false
66
+ findSymbolArray = []
67
+ board.each_with_index do |block,index|
68
+ jump = index
69
+ count = 1
70
+ if block.include?(symbol)
71
+ loop do
72
+ findSymbolArray << board[jump].include?(symbol) if count <= @@sizeBoard and jump < board.length
73
+ if count == @@sizeBoard
74
+ playerWon = findSymbolArray.all? {|findValue| findValue == true}
75
+ break
76
+ end
77
+ count+=1
78
+ jump+=(@@sizeBoard+1)
79
+ break if jump >= board.length
80
+ end
81
+ findSymbolArray = []
82
+ break if playerWon
83
+ end
84
+ end
85
+ unless playerWon
86
+ playerWon = diagonalInversa(board,symbol)
87
+ end
88
+ congratulationPlayer(board,symbol) if playerWon
89
+ return playerWon
90
+ end
91
+
92
+ #To check the other Diagonal way to win the game
93
+ private def diagonalInversa(board,symbol)
94
+ playerWon = false
95
+ findSymbolArray = []
96
+ jump = @@sizeBoard-1
97
+ count = 1
98
+ loop do
99
+ findSymbolArray << board[jump].include?(symbol) if count <= @@sizeBoard
100
+ if count == @@sizeBoard
101
+ playerWon = findSymbolArray.all? {|findValue| findValue == true}
102
+ break
103
+ end
104
+ count+=1
105
+ jump+=(@@sizeBoard-1)
106
+ break if playerWon
107
+ break if jump >= board.length
108
+ end
109
+ return playerWon
110
+ end
111
+
112
+ #To check if the game ends because the players tie
113
+ private def checkTie(board)
114
+ countSymbol = []
115
+ board.each do |block|
116
+ countSymbol << true if block.include?('X') || block.include?('O')
117
+ end
118
+ congratulationTie if countSymbol.length == (@@sizeBoard*@@sizeBoard)
119
+ return countSymbol.length == (@@sizeBoard*@@sizeBoard)
120
+ end
121
+
122
+ #To send a message to the player who won
123
+ private def congratulationPlayer(board,symbol)
124
+ system('clear')
125
+ 3.times{
126
+ puts "======================================================================"
127
+ puts " W I N N E R P L A Y E R '#{symbol}'".colorize(:green)
128
+ puts "======================================================================"
129
+ puts " C O N G R A T U L A T I O N S"
130
+ puts "======================================================================"
131
+ puts
132
+ }
133
+ end
134
+
135
+ private def congratulationTie
136
+ system('clear')
137
+ 3.times{
138
+ puts "======================================================================"
139
+ puts " T I E ".colorize(:red)
140
+ puts "======================================================================"
141
+ puts " C O N G R A T U L A T I O N S"
142
+ puts "======================================================================"
143
+ puts
144
+ }
145
+ end
146
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ttt_chido
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Saul Rubio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.1
55
+ description: It's a cool gem to have fun some time.
56
+ email:
57
+ - rubiocanino98@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - bin/console
63
+ - bin/setup
64
+ - lib/game.rb
65
+ - lib/ttt_chido.rb
66
+ - lib/ttt_chido/version.rb
67
+ - lib/winner.rb
68
+ homepage: https://github.com/rubiocanino/ttt_chido
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ allowed_push_host: https://rubygems.org
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.5.2.1
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: This a gem it was made to play Tic Tac Toe in the irb.
93
+ test_files: []