-
Notifications
You must be signed in to change notification settings - Fork 30
Kenworthy #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mrk3767
wants to merge
5
commits into
master
Choose a base branch
from
Kenworthy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Kenworthy #5
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class TooMuchContent < Exception | ||
| end | ||
| class NotInitialized < Exception | ||
| end | ||
| class NotImplemented < Exception | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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; endif you want to convert these into oneliners.