TicTacToe_Col 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d5f2f075fb77caab2b7cd0a4160376167e7872cc24ae15401cf75f6565a5bc7c
4
+ data.tar.gz: aef712de7cff41405978cb7eaeb2ec9c82ee35123238f953a2996353547c529c
5
+ SHA512:
6
+ metadata.gz: 2958a6faa2366a6d1458f7658bf2728abf25a872de50e938a1a8e76894a4c6afbcab442edfed798828274e1aa1c6dd9d1ce886211b2b3be5e27666bf629ef279
7
+ data.tar.gz: 13e6aa31bc6b4cf71102ac39574222df84f25e34759bf8c8d8764242f4bdeb556963874746f6a2f9ac0987b0596e444488dd3ebbf0e9ed3d487a28baf003596c
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "TicTacToe_Col"
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__)
@@ -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
@@ -0,0 +1,51 @@
1
+ require "TicTacToe_Col/version"
2
+ require 'colorize'
3
+ require 'ttt'
4
+
5
+ module TicTacToeCol
6
+ @@ttt = TicTacToe.new
7
+ def start
8
+ loop do
9
+ puts " ------------- MENU ------------- ".cyan
10
+ puts "| [1] Play Tic Tac Toe |".cyan
11
+ puts "| [2] Exit |".cyan
12
+ puts " -------------------------------- ".cyan
13
+ first_menu = gets.chomp.to_i
14
+ case first_menu
15
+ when 1
16
+ default_player = 1
17
+ @@ttt.start_game(default_player)
18
+ player = @@ttt.turns
19
+ loop do
20
+ puts "-------------------------------------- ".cyan
21
+ puts "| Do you want to start a new game? |".cyan
22
+ puts "| [1] Continue |".cyan
23
+ puts "| [2] Show Scoreboard |".cyan
24
+ puts "| [3] Return to first menu |".cyan
25
+ puts "-------------------------------------- ".cyan
26
+ second_menu = gets.chomp.to_i
27
+ case second_menu
28
+ when 1
29
+ @@ttt.start_game(player)
30
+ player = @@ttt.turns
31
+ when 2
32
+ @@ttt.check_winners
33
+ when 3
34
+ break
35
+ else
36
+ puts "Choose a valid element".red
37
+ end
38
+ end
39
+ when 2
40
+ break
41
+ else
42
+ puts "Choose a valid element".red
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ class Main
49
+ include TicTacToeCol
50
+ end
51
+ # Main.new.start
@@ -0,0 +1,3 @@
1
+ module TicTacToeCol
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,218 @@
1
+ require 'matrix'
2
+ require 'colorize'
3
+ class TicTacToe
4
+ def initialize()
5
+ @winners = []
6
+ end
7
+ #Start the game
8
+ def start_game(player)
9
+ @player_a_count = 0
10
+ @player_b_count = 0
11
+ print "\nCreate Board, What length do you want?: ".cyan
12
+ board_length = gets.chomp.to_i
13
+ if board_length.nil? || board_length == 0
14
+ puts "Invalid element, try again"
15
+ start_game(player)
16
+ end
17
+ board = Array.new(board_length){|i| Array.new(board_length){|j| board_length*i+j+1 } }
18
+ board_size = board_length * board_length
19
+ puts "Board created!!!... \n".green
20
+ display_board(board,board_length)
21
+ play(board, board_size, board_length,player)
22
+ end
23
+ #Show the game board
24
+ def display_board(board, length)
25
+ count_jump = 0
26
+ count_position = 0
27
+ board.each do |x|
28
+ x.each do |y|
29
+ count_position += 1
30
+ if count_position % length == 0
31
+ count_jump += 1
32
+ print y == "X" || y == "O" ? " #{add_space(y)}#{y} \n".white : " #{add_space(y)}#{y} \n".magenta
33
+ draw_lines(length) if count_jump < length
34
+ puts
35
+ else
36
+ print y == "X" || y == "O" ? " #{add_space(y)}#{y} ".white : " #{add_space(y)}#{y} ".magenta
37
+ print "|".magenta
38
+ end
39
+ end
40
+ end
41
+ end
42
+ #Add space to the element depending on the number, if the number is one digit it will add a space
43
+ def add_space(value)
44
+ if value.to_i <= 9 || value.to_s == "X" || value.to_s == "O"
45
+ print " "
46
+ end
47
+ end
48
+ #Help to display_board to draw the board to be symmetrical
49
+ def draw_lines(length)
50
+ if length == 3
51
+ 14.times { print "-".magenta }
52
+ else
53
+ lines = (length - 3) * 5
54
+ (14 + lines).times { print "-".magenta }
55
+ end
56
+ end
57
+ #Logic of the game where the player's turn is valided
58
+ def play(board, board_size, board_length, player)
59
+ puts "The game has started".green
60
+ index = player.to_i
61
+ if player.to_i == 1
62
+ board_size += 1
63
+ end
64
+ while index < board_size
65
+ if index.odd?
66
+ print "\nPlayer A -- Pick a number: ".cyan
67
+ value = gets.chomp
68
+ puts
69
+ player_a = player_a(board, value)
70
+ display_board(board, board_length.to_i)
71
+ sign = "X"
72
+ @player_a_count += 1
73
+ if winner(board, sign, board_length)
74
+ puts "PLAYER A - Congratulations!!! You are the winner\n".green
75
+ @winners << "X"
76
+ @player_a_count = 0
77
+ break
78
+ end
79
+ unless player_a
80
+ puts "Invalid number, please try again".red
81
+ index -= 1
82
+ @player_a_count -= 1
83
+ end
84
+ puts "In Draw!!!".green if (index + 1) == board_size
85
+ @winners << "In Draw"
86
+ else
87
+ print "\nPlayer B -- Pick a number: ".cyan
88
+ value = gets.chomp
89
+ puts
90
+ player_b = player_b(board, value)
91
+ display_board(board, board_length.to_i)
92
+ sign = "O"
93
+ @player_b_count += 1
94
+ if winner(board, sign, board_length)
95
+ puts "PLAYER B - Congratulations!!! You are the winner\n".green
96
+ @winners << "O"
97
+ @player_b_count = 0
98
+ break
99
+ end
100
+ unless player_b
101
+ puts "Invalid number, please try again".red
102
+ index -= 1
103
+ @player_b_count -= 1
104
+ end
105
+ puts "In Draw!!!".green if (index + 1) == board_size
106
+ end
107
+ index += 1
108
+ end
109
+ end
110
+ #Insert an "X" in the cell chosen by the player "player_a"
111
+ def player_a(board, value)
112
+ board.each.with_index do |row, i|
113
+ row.each.with_index do |cell, j|
114
+ if board[i][j]==value.to_i
115
+ board[i][j] = "X"
116
+ return true
117
+ break
118
+ end
119
+ end
120
+ end
121
+ return false
122
+ end
123
+ #Insert an "O" in the cell chosen by the player "player_b"
124
+ def player_b(board, value)
125
+ board.each.with_index do |row, i|
126
+ row.each.with_index do |cell, j|
127
+ if board[i][j] == value.to_i
128
+ board[i][j] = "O"
129
+ return true
130
+ break
131
+ end
132
+ end
133
+ end
134
+ return false
135
+ end
136
+ #Check on the board if the player won in any of the rows
137
+ def winner_row(board, value, length)
138
+ m_board = Matrix[*board]
139
+ i = 0
140
+ while i < length.to_i
141
+ winner = m_board.row(i).all? {|v| v == value}
142
+ if winner
143
+ return true
144
+ break
145
+ end
146
+ i += 1
147
+ end
148
+ end
149
+ #Check on the board if the player won in any of the columns
150
+ def winner_column(board, value, length)
151
+ m_board = Matrix[*board]
152
+ i = 0
153
+ while i < length.to_i
154
+ winner = m_board.column(i).all? {|v| v == value}
155
+ if winner
156
+ return true
157
+ break
158
+ end
159
+ i += 1
160
+ end
161
+ end
162
+ #check on the board if the player won on the main diagonal
163
+ def winner_diagonal(board, value, length)
164
+ m_board = Matrix[*board]
165
+ count = 0
166
+ m_board.each_with_index do |e, row, col|
167
+ if row == col
168
+ if e == value
169
+ count += 1
170
+ end
171
+ end
172
+ end
173
+ return true if count == length.to_i
174
+ end
175
+ #check on the board if the player won the reverse diagonal
176
+ def winner_diagonal_inverse(board, value, length)
177
+ m_board = Matrix[*board]
178
+ count = 0
179
+ m_board.square? && (0...m_board.row_size).each do |i|
180
+ if m_board[i, m_board.row_size - 1 - i] == value
181
+ count += 1
182
+ if count == length.to_i
183
+ return true
184
+ break
185
+ end
186
+ end
187
+ end
188
+ end
189
+ #returns true if there is a winner
190
+ def winner(board, value, length)
191
+ if winner_row(board, value, length) || winner_column(board, value, length) || winner_diagonal(board, value, length) || winner_diagonal_inverse(board, value, length)== true
192
+ return true
193
+ end
194
+ end
195
+ #Shows how many times a player has won
196
+ def check_winners
197
+ winner_x = @winners.count('X')
198
+ winner_o = @winners.count('O')
199
+ puts "X has won #{winner_x} times"
200
+ puts "O has won #{winner_o} times"
201
+ end
202
+ #Shows the player that will start the next game
203
+ def turns
204
+ player_a = @player_a_count; # 1
205
+ player_b = @player_b_count; # 0
206
+ if player_a == 0
207
+ return 0
208
+ elsif player_b == 0
209
+ return 1
210
+ else
211
+ if player_a < player_b
212
+ return 1
213
+ else
214
+ return 0
215
+ end
216
+ end
217
+ end
218
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: TicTacToe_Col
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ismael Villavicencio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-30 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: matrix
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.1
69
+ description: Two players represented with X and O, first time the game is played,
70
+ X starts. Each player alternate turns to put a mark in the board on any available
71
+ slot. The game ends when either one of the players matches three marks in a horizontal,
72
+ vertical or diagonal row or there are no more moves available, once the game finishes,
73
+ players are asked if they want to play again. If they do, the player who lost the
74
+ previous match starts. In case of a draw, the player who did the second-to-last
75
+ movement starts
76
+ email:
77
+ - villavicencioismael@gmail.com
78
+ executables:
79
+ - console
80
+ - setup
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/TicTacToe_Col.rb
87
+ - lib/TicTacToe_Col/version.rb
88
+ - lib/ttt.rb
89
+ homepage: https://github.com/IsmaelVillavicencio/TicTacToe_Col
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.7.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Tic Tac Toe is an awesome gem.
113
+ test_files: []