Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions tictactoe/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require_relative 'globals'

class GameController
include GameView
include GameState
include GameModel

def run!
# Print::display("Mikes page!" , ["Some stuff is crazy", "and that is why I love it", "be peaceful my friend"], "~Kenworthy", 80)
@currState = InitState.new
Print.initialize('Hello world!', '~Kenworthy inc', 80)

until @currState.is_a? EndState

Print.display(@currState.getBody)

nextState = @currState.send(Print.getInput(@currState.getQuestion))

(nextState = Complete.new if checkFTW(Model.board) || Model.board.full?) if @currState.is_a? Turn

@currState = nextState
end
end

def checkFTW(board)
puts 'Heres the board!'
p board.data

winBoard = Array.new(3) { Array.new(board.size) { nil } }
(board.size - 1).times do |i|
winBoard[0][i] = board.data[i][0]
winBoard[1][i] = board.data[0][i]
end
winBoard[2][0] = board.data[0][0]
winBoard[2][1] = board.data[0][board.size - 1]

# puts "Heres the unchecked winners board!"
# p winBoard

board.size.times do |r|
board.size.times do |c|
# puts "Row #{r} Col #{c}"
# puts "Winboard value #{winBoard[1][c]} Board value #{board.data[r][c]}"
winBoard[1][c] = false unless board.data[r][c] && winBoard[1][c] && board.data[r][c].class == winBoard[1][c].class
winBoard[0][r] = false unless board.data[r][c] && winBoard[0][r] && board.data[r][c].class == winBoard[0][r].class

if c == r
winBoard[2][0] = false unless board.data[r][c] && winBoard[2][0] && board.data[r][c].class == winBoard[2][0].class
end
if (c + r) == (board.size - 1)
winBoard[2][1] = false unless board.data[r][c] && winBoard[2][1] && board.data[r][c].class == winBoard[2][1].class
end
end
end

# puts "here are the winners!"
puts 'There was a winner?'
p winBoard.flatten.any?
p winBoard
rcd = -1
num = -1
if winBoard.flatten.any?
winBoard.size.times do |c|
winBoard[c].size.times do |r|
if winBoard[r][c]
Model.winning_position = [r, c]
Model.winner = winBoard[r][c].owner
return true
end
end
end
end
false
end
end

GameController.new.run!
6 changes: 6 additions & 0 deletions tictactoe/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class TooMuchContent < Exception
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also do this... class TooMuchContent < Exception; end if you want to convert these into oneliners.

end
class NotInitialized < Exception
end
class NotImplemented < Exception
end
4 changes: 4 additions & 0 deletions tictactoe/globals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require_relative 'view'
require_relative 'model'
require_relative 'states'
require_relative 'exceptions'
92 changes: 92 additions & 0 deletions tictactoe/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
require_relative 'globals'

module GameModel
module Model
class << self
attr_reader :board
attr_accessor :winner, :winning_position
@initilized = false

def initialize
@players = [nil, nil]
@board = Board.new
@initilized = true
@winner = nil
end

def set_player(name, p)
@players[p] = Player.new(name, p == 0 ? X.new(self) : O.new(self))
end

def get_player(i)
@players[i] ? @players[i] : 'not set'
end

def getBoardAsArray
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend using snake_case over camelCase for method and variable names.

string = [" \e[4m [0] [1] [2] \e[0m "]
@board.size.times do |r|
row = "[#{r}] |"
@board.size.times do |c|
row += "\e[4m " + (@board.data[r][c] ? @board.data[r][c].to_s : ' ') + " \e[0m|"
end
row += ' '
string.push(row)
end
puts string
string
end

def place_piece(piece, r, c)
@board.data[r][c] = piece
end

def get_space(r, c)
@board.data[r][c]
end
end
end

class Player
attr_reader :piece
def initialize(name, piece)
@name = name
@piece = piece
end

def to_s
@name + '(' + @piece.to_s + ')'
end
end

class Piece
attr_reader :owner
def initialize(owner)
@owner = owner
end
end

class X < Piece
def to_s
'X'
end
end

class O < Piece
def to_s
'O'
end
end

class Board
attr_accessor :data, :size
def initialize(size = 3)
@size = size
# @data = [[X.new(P1.new("Mike")),nil,nil],[X.new(P1.new("Mike")),X.new(P1.new("Mike")),nil],[X.new(P1.new("Mike")),nil,X.new(P1.new("Mike"))]]#Array.new(size) { Array.new(size) { nil }}#X.new(P2.new("Mike")) } }
@data = Array.new(size) { Array.new(size) { nil } }
end

def full?
@data.flatten.reduce(true) { |m, i| m && i }
end
end
end
Loading