TicTacToe 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/Display.rb +76 -0
- data/lib/Gato.rb +138 -0
- metadata +46 -0
data/lib/Display.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require_relative "Gato.rb"
|
2
|
+
|
3
|
+
class Display
|
4
|
+
|
5
|
+
@TicTacToe
|
6
|
+
def initialize
|
7
|
+
@TicTacToe = Gato.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def play
|
11
|
+
print_rules
|
12
|
+
while not @TicTacToe.isFinished
|
13
|
+
print @TicTacToe.currentPlayer, "'s turn >>>"
|
14
|
+
print_board
|
15
|
+
new_row = get_row_to_move
|
16
|
+
new_column = get_column_to_move
|
17
|
+
move_result = @TicTacToe.intended_move(new_row, new_column)
|
18
|
+
put_result_operation(move_result)
|
19
|
+
end
|
20
|
+
print_board
|
21
|
+
end
|
22
|
+
|
23
|
+
def print_rules
|
24
|
+
puts "******TIC TAC TOE********"
|
25
|
+
puts "Objective of the game: get 3 X's or O's in a row, column or diagonal"
|
26
|
+
puts "Each player will make a move and then, the other player will play until"
|
27
|
+
puts "one of the players win or a draw is reached."
|
28
|
+
puts "The X will go first."
|
29
|
+
end
|
30
|
+
|
31
|
+
def print_board
|
32
|
+
puts ''
|
33
|
+
puts "_______ _______ _______"
|
34
|
+
for row in (0..2)
|
35
|
+
puts " | | |"
|
36
|
+
for column in (0..2)
|
37
|
+
casilla = @TicTacToe.board[row][column]
|
38
|
+
print " ", casilla, " |"
|
39
|
+
end
|
40
|
+
puts ''
|
41
|
+
puts "_______|_______|_______|"
|
42
|
+
end
|
43
|
+
puts ''
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_row_to_move
|
47
|
+
print "Row [1-3]: "
|
48
|
+
new_row = gets.chomp
|
49
|
+
new_row.to_i
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_column_to_move
|
53
|
+
print "Column [1-3]: "
|
54
|
+
new_column = gets.chomp
|
55
|
+
new_column.to_i
|
56
|
+
end
|
57
|
+
|
58
|
+
def put_result_operation(operation_number)
|
59
|
+
case operation_number
|
60
|
+
when -2#Gato:BOX_TAKEN
|
61
|
+
puts "That box is taken, try another one"
|
62
|
+
when -1 #Gato:INVALID_POSITION
|
63
|
+
puts "The Position you choose is out of the board, try another one"
|
64
|
+
when 0 #Gato:SUCCESSFUL_MOVE
|
65
|
+
puts "The move was made."
|
66
|
+
when 1 #Gato:GAME_FINISHED
|
67
|
+
puts "The game ended"
|
68
|
+
print "The winner was player ", @TicTacToe.currentPlayer
|
69
|
+
when 2 #Gato:GAME_TIED
|
70
|
+
puts "The game resulted in a tie, better luck next time"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
TTT_Game = Display.new
|
76
|
+
TTT_Game.play
|
data/lib/Gato.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
MAX_MOVES = 9
|
2
|
+
SIZE = 3
|
3
|
+
EMPTY = '-'
|
4
|
+
CROSS = 'X'
|
5
|
+
BALL = 'O'
|
6
|
+
BOX_TAKEN = -2
|
7
|
+
INVALID_POSITION = -1
|
8
|
+
SUCCESSFUL_MOVE = 0
|
9
|
+
GAME_FINISHED = 1
|
10
|
+
GAME_TIED = 2
|
11
|
+
class Gato
|
12
|
+
attr_accessor :board, :isFinished, :currentPlayer
|
13
|
+
|
14
|
+
@board
|
15
|
+
@currentPlayer
|
16
|
+
@isFinished
|
17
|
+
@quantity_of_moves
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@board = [[EMPTY,EMPTY,EMPTY],[EMPTY, EMPTY, EMPTY],[EMPTY, EMPTY, EMPTY]]
|
21
|
+
@currentPlayer = CROSS
|
22
|
+
@isFinished = false
|
23
|
+
@quantity_of_moves = 0
|
24
|
+
end
|
25
|
+
|
26
|
+
def intended_move(row, column)
|
27
|
+
result_operation = 0
|
28
|
+
if not is_in_bounderies?(row, column)
|
29
|
+
result_operation = INVALID_POSITION
|
30
|
+
elsif not is_empty?(row-1, column-1)
|
31
|
+
result_operation = BOX_TAKEN
|
32
|
+
else
|
33
|
+
result_operation = update_game(row-1, column-1)
|
34
|
+
end
|
35
|
+
result_operation
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def is_there_a_draw?
|
41
|
+
@quantity_of_moves == MAX_MOVES
|
42
|
+
end
|
43
|
+
|
44
|
+
def is_there_a_winner?(row, column)
|
45
|
+
is_there_a_winner_on_row?(row) or is_there_a_winner_on_column?(column) or is_there_a_winner_on_diagonal? or is_there_a_winner_on_antidiagonal?
|
46
|
+
end
|
47
|
+
|
48
|
+
def is_there_a_winner_on_row?(row)
|
49
|
+
result = false
|
50
|
+
for column in (0..SIZE - 1)
|
51
|
+
if not @board[row][column] == @currentPlayer
|
52
|
+
break
|
53
|
+
end
|
54
|
+
if column == SIZE - 1
|
55
|
+
result = true
|
56
|
+
end
|
57
|
+
end
|
58
|
+
result
|
59
|
+
end
|
60
|
+
|
61
|
+
def is_there_a_winner_on_column?(column)
|
62
|
+
result = false
|
63
|
+
for row in (0..SIZE - 1)
|
64
|
+
if not @board[row][column] == @currentPlayer
|
65
|
+
break
|
66
|
+
end
|
67
|
+
if row == SIZE - 1
|
68
|
+
result = true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
result
|
72
|
+
end
|
73
|
+
|
74
|
+
def is_there_a_winner_on_diagonal?
|
75
|
+
result = false
|
76
|
+
for position in (0..SIZE - 1)
|
77
|
+
if not @board[position][position] == @currentPlayer
|
78
|
+
break
|
79
|
+
end
|
80
|
+
if position == SIZE - 1
|
81
|
+
result = true
|
82
|
+
end
|
83
|
+
end
|
84
|
+
result
|
85
|
+
end
|
86
|
+
|
87
|
+
def is_there_a_winner_on_antidiagonal?
|
88
|
+
result = false
|
89
|
+
for position in (0..SIZE - 1)
|
90
|
+
if not @board[position][2-position] == @currentPlayer
|
91
|
+
break
|
92
|
+
end
|
93
|
+
if position == SIZE - 1
|
94
|
+
result = true
|
95
|
+
end
|
96
|
+
end
|
97
|
+
result
|
98
|
+
end
|
99
|
+
|
100
|
+
def make_move(row, column)
|
101
|
+
@board[row][column] = @currentPlayer
|
102
|
+
@quantity_of_moves += 1
|
103
|
+
end
|
104
|
+
|
105
|
+
def update_game(row, column)
|
106
|
+
result_operation = 0
|
107
|
+
make_move(row, column)
|
108
|
+
if is_there_a_draw?
|
109
|
+
@isFinished = true
|
110
|
+
result_operation = GAME_TIED
|
111
|
+
elsif is_there_a_winner?(row, column)
|
112
|
+
@isFinished = true
|
113
|
+
result_operation = GAME_FINISHED
|
114
|
+
end
|
115
|
+
if not @isFinished
|
116
|
+
change_current_player
|
117
|
+
result_operation = SUCCESSFUL_MOVE
|
118
|
+
end
|
119
|
+
result_operation
|
120
|
+
end
|
121
|
+
|
122
|
+
def is_in_bounderies?(row, column)
|
123
|
+
row > 0 and row <= SIZE and
|
124
|
+
column > 0 and row <= SIZE
|
125
|
+
end
|
126
|
+
|
127
|
+
def is_empty?(row, column)
|
128
|
+
@board[row][column] == EMPTY
|
129
|
+
end
|
130
|
+
|
131
|
+
def change_current_player
|
132
|
+
if @currentPlayer == CROSS
|
133
|
+
@currentPlayer = BALL
|
134
|
+
else
|
135
|
+
@currentPlayer = CROSS
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: TicTacToe
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ignacio Coto M.
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-02 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Gem of the Tic Tac Toe Game
|
15
|
+
email: ignacio.coto.mata@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/Display.rb
|
21
|
+
- lib/Gato.rb
|
22
|
+
homepage: http://rubygems.org/gems/tic_tac_toe_ignacioCoto
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.23
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Tic Tac Toe game
|
46
|
+
test_files: []
|