mastermind; basic logic, color highlighting, etc

This commit is contained in:
Mikayla Dobson
2023-05-29 22:37:51 -05:00
parent fdb5b211b3
commit a347e5a776
10 changed files with 177 additions and 54 deletions

View File

@@ -1,13 +1,5 @@
class BoardBase
attr_accessor :turn
def initialize(turn = 1)
@turn = turn
end
def to_s
print "
Current turn: #{@turn}\n
"
module BoardBase
def check_for_winner
fail NotImplementedError, "Board must implement a 'check_for_winner' method"
end
end

View File

@@ -1,6 +1,6 @@
class GameBase
def initialize
puts "Welcome to Tic Tac Toe!"
module GameBase
def initialize(game_name = "(unspecified game)")
puts "Welcome to #{game_name}!"
puts "Player 1, what is your name?"
@player_one_name = gets.chomp
@@ -10,7 +10,7 @@ class GameBase
@playerOne = Player.new(@player_one_name)
@playerTwo = Player.new(@player_two_name)
@statusMessage = ""
@board = Board.new()
@board = Board.new
@winner = nil
@turn = 1
end
@@ -24,4 +24,28 @@ class GameBase
#{@board.to_s}
"
end
def play
until @winner
self.turn
end
@statusMessage = "#{@statusMessage + "\nPress 'x' to exit or 'a' to play again."}"
print self.to_s
if gets.chomp == "a"
@board = Board.new
@winner = nil
@statusMessage = ""
@turn = 1
self.play()
elsif gets.chomp != "x"
@statusMessage = "Please provide a valid input"
print self.to_s
end
end
def turn
fail NotImplementedError, "Game must implement a 'turn' method"
end
end

26
common/String.rb Normal file
View File

@@ -0,0 +1,26 @@
# https://stackoverflow.com/questions/1489183/how-can-i-use-ruby-to-colorize-the-text-output-to-a-terminal
class String
def black; "\e[30m#{self}\e[0m" end
def red; "\e[31m#{self}\e[0m" end
def green; "\e[32m#{self}\e[0m" end
def brown; "\e[33m#{self}\e[0m" end
def blue; "\e[34m#{self}\e[0m" end
def magenta; "\e[35m#{self}\e[0m" end
def cyan; "\e[36m#{self}\e[0m" end
def gray; "\e[37m#{self}\e[0m" end
def bg_black; "\e[40m#{self}\e[0m" end
def bg_red; "\e[41m#{self}\e[0m" end
def bg_green; "\e[42m#{self}\e[0m" end
def bg_brown; "\e[43m#{self}\e[0m" end
def bg_blue; "\e[44m#{self}\e[0m" end
def bg_magenta; "\e[45m#{self}\e[0m" end
def bg_cyan; "\e[46m#{self}\e[0m" end
def bg_gray; "\e[47m#{self}\e[0m" end
def bold; "\e[1m#{self}\e[22m" end
def italic; "\e[3m#{self}\e[23m" end
def underline; "\e[4m#{self}\e[24m" end
def blink; "\e[5m#{self}\e[25m" end
def reverse_color; "\e[7m#{self}\e[27m" end
end