some solutions for projects on odin project
This commit is contained in:
78
tic_tac_toe/Board.rb
Normal file
78
tic_tac_toe/Board.rb
Normal file
@@ -0,0 +1,78 @@
|
||||
class Board
|
||||
attr_accessor :turn, :squares, :update_square
|
||||
|
||||
def initialize(turn = 1)
|
||||
@turn = turn
|
||||
@squares = [
|
||||
[" ", " ", " "],
|
||||
[" ", " ", " "],
|
||||
[" ", " ", " "]
|
||||
]
|
||||
end
|
||||
|
||||
def to_s
|
||||
print "\n
|
||||
1: #{@squares[0][0]} | 2: #{@squares[0][1]} | 3: #{@squares[0][2]} \n
|
||||
---------------------\n
|
||||
4: #{@squares[1][0]} | 5: #{@squares[1][1]} | 6: #{@squares[1][2]} \n
|
||||
---------------------\n
|
||||
7: #{@squares[2][0]} | 8: #{@squares[2][1]} | 9: #{@squares[2][2]} \n
|
||||
"
|
||||
end
|
||||
|
||||
def update_square(square, symbol)
|
||||
# translate a numbered square onto a matrix
|
||||
row = ((square - 1) / 3).ceil()
|
||||
column = (square - 1) % 3
|
||||
success = false
|
||||
|
||||
if @squares[row][column] != " "
|
||||
puts "Square is already taken."
|
||||
puts "Please try again."
|
||||
else
|
||||
@squares[row][column] = symbol
|
||||
success = true
|
||||
end
|
||||
|
||||
success
|
||||
end
|
||||
|
||||
def check_for_winner
|
||||
winner = nil
|
||||
|
||||
# check whether all squares in a row match
|
||||
for row in @squares
|
||||
if row.all? { |square| square == "X" }
|
||||
if !winner
|
||||
winner = "X"
|
||||
end
|
||||
elsif row.all? { |square| square == "O" }
|
||||
if !winner
|
||||
winner = "O"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# check whether all columns match
|
||||
if not winner
|
||||
for column in 0..2
|
||||
if @squares[column].all? { |square| square == "X" }
|
||||
winner = "X"
|
||||
elsif @squares[column].all? { |square| square == "O" }
|
||||
winner = "O"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# check for diagonal matches
|
||||
if not winner
|
||||
if (@squares[0][0] == "X" or @squares[0][0] == "O") and @squares[0][0] == @squares[1][1] and @squares[1][1] == @squares[2][2]
|
||||
winner = @squares[0][0]
|
||||
elsif (@squares[0][2] == "X" or @squares[0][2] == "O") and @squares[0][2] == @squares[1][1] and @squares[1][1] == @squares[2][0]
|
||||
winner = @squares[0][2]
|
||||
end
|
||||
end
|
||||
|
||||
winner
|
||||
end
|
||||
end
|
||||
70
tic_tac_toe/Game.rb
Normal file
70
tic_tac_toe/Game.rb
Normal file
@@ -0,0 +1,70 @@
|
||||
class Game
|
||||
def initialize
|
||||
puts "Welcome to Tic Tac Toe!"
|
||||
puts "Player 1, what is your name?"
|
||||
player_one_name = gets
|
||||
|
||||
puts "Player 2, what is your name?"
|
||||
player_two_name = gets
|
||||
|
||||
@playerOne = Player.new(player_one_name, "X")
|
||||
@playerTwo = Player.new(player_two_name, "O")
|
||||
@statusMessage = ""
|
||||
@board = Board.new()
|
||||
@winner = nil
|
||||
@turn = 1
|
||||
end
|
||||
|
||||
def to_s
|
||||
print "
|
||||
Player one: #{@playerOne.name}\n
|
||||
Player two: #{@playerTwo.name}\n
|
||||
Current turn: #{@turn}\n
|
||||
#{@statusMessage}
|
||||
#{@board.to_s}
|
||||
"
|
||||
end
|
||||
|
||||
def play
|
||||
until @winner
|
||||
self.turn()
|
||||
end
|
||||
|
||||
@statusMessage = "#{@statusMessage + "\nPress 'x' to exit"}"
|
||||
print self.to_s
|
||||
|
||||
until gets.chomp == "x"
|
||||
print self.to_s
|
||||
end
|
||||
end
|
||||
|
||||
def turn()
|
||||
# clear the terminal on each turn
|
||||
puts "\e[H\e[2J"
|
||||
print self.to_s
|
||||
|
||||
puts "Player #{@turn % 2 == 0 ? @playerTwo.name : @playerOne.name}, please select a square. (1-9)"
|
||||
player_input = gets.chomp.to_i
|
||||
|
||||
unless player_input.is_a?(Numeric) and player_input.between?(1, 9)
|
||||
@statusMessage = "Invalid input. Expected a number between 1 and 9."
|
||||
self.turn()
|
||||
end
|
||||
|
||||
move_success = @board.update_square(player_input, @turn % 2 == 0 ? "O" : "X")
|
||||
|
||||
if not move_success
|
||||
@statusMessage = "Invalid input. This square is already taken."
|
||||
self.turn()
|
||||
end
|
||||
|
||||
winner_this_turn = @board.check_for_winner
|
||||
|
||||
if winner_this_turn
|
||||
@winner = (winner_this_turn == "X" ? @playerOne.name : @playerTwo.name)
|
||||
@statusMessage = "#{@winner} wins!"
|
||||
else
|
||||
@turn += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
10
tic_tac_toe/Player.rb
Normal file
10
tic_tac_toe/Player.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
class Player
|
||||
attr_accessor :turn_active
|
||||
attr_reader :name, :symbol, :turn_active
|
||||
|
||||
def initialize(name, symbol)
|
||||
@name = name
|
||||
@symbol = symbol
|
||||
@turn_active = false
|
||||
end
|
||||
end
|
||||
6
tic_tac_toe/main.rb
Normal file
6
tic_tac_toe/main.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
require_relative "Board.rb"
|
||||
require_relative "Player.rb"
|
||||
require_relative "Game.rb"
|
||||
|
||||
game = Game.new
|
||||
game.play
|
||||
Reference in New Issue
Block a user