Created
August 24, 2011 03:12
-
-
Save samccone/1167225 to your computer and use it in GitHub Desktop.
connect4
This file contains 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
class String | |
def is_i? | |
!!(self =~ /^[-+]?[0-9]+$/) | |
end | |
end | |
class Board | |
def initialize(rows,columns) | |
@theBoard = Hash.new | |
@rows = rows | |
@columns = columns | |
end #end initialize | |
def drawBoard() | |
theOutput = "" | |
(0..@columns-1).each do |k| | |
theOutput+="=#{k}= " | |
end | |
theOutput+="\n" | |
(0..@rows-1).each do |i| | |
(0..@columns-1).each do |j| | |
if(@theBoard[[i,j]]==nil) | |
theOutput+="| | " | |
else | |
theOutput+="|#{@theBoard[[i,j]].piece}| " | |
end | |
end | |
theOutput+="\n" | |
end | |
return theOutput | |
end #end drawBoard | |
def addPiece(row,col,piece) | |
if(isEmpty(row,col)) | |
@theBoard[[row,col]] = piece | |
return true | |
else | |
return false | |
end | |
end #end addPiece | |
def dropPiece(col,piece) | |
row = @rows-1 | |
until (row<0) | |
if(isEmpty(row,col)) | |
return addPiece(row,col,piece) | |
end | |
row-=1 | |
end | |
return false | |
end | |
def isEmpty(row,col) | |
return withinBounds(row,col) && @theBoard[[row,col]]==nil | |
end #end isEmpty | |
def withinBounds(row,col) | |
return row < @rows && col < @columns | |
end #end withinBounds | |
def isFull(row,col) | |
return withinBounds(row,col) && @theBoard[[row,col]]!=nil | |
end | |
def boardFull() | |
i=0 | |
while(i<@columns) | |
if(@theBoard[[0,i]]==nil) | |
return false | |
end | |
i+=1 | |
end | |
return true | |
end | |
def stepThrough(direction,current,i,j) | |
k=0 | |
toReturn=true | |
while(k<4) | |
if(direction=='up') | |
if(!current.compare(@theBoard[[i+k,j]])) | |
toReturn=false | |
break | |
end | |
k+=1 | |
end | |
if(direction=='right') | |
if(!current.compare(@theBoard[[i,j+k]])) | |
toReturn=false | |
break | |
end | |
k+=1 | |
end | |
if(direction=='diagright') | |
if(!current.compare(@theBoard[[i-k,j+k]])) | |
toReturn=false | |
break | |
end | |
k+=1 | |
end | |
if(direction=='diagleft') | |
if(!current.compare(@theBoard[[i-k,j-k]])) | |
toReturn=false | |
break | |
end | |
k+=1 | |
end | |
end | |
return toReturn | |
end | |
def fourConnected() | |
toReturn = [] | |
(0..@rows-1).each do |i| | |
(0..@columns-1).each do |j| | |
if(isFull(i,j)) | |
current = @theBoard[[i,j]] | |
if(isFull(i+3,j)) #up | |
if(stepThrough('up',current,i,j)) | |
return true | |
end | |
end | |
if(isFull(i,j+3)) #right | |
if(stepThrough('right',current,i,j)) | |
return true | |
end | |
end | |
if(isFull(i-3,j+3)) #diagright | |
if(stepThrough('diagright',current,i,j)) | |
return true | |
end | |
end | |
if(isFull(i-3,j-3)) #diagleft | |
if(stepThrough('diagleft',current,i,j)) | |
return true | |
end | |
end | |
end | |
end | |
end | |
return false | |
end | |
end | |
class Piece | |
def initialize(color,piece='x') | |
@color = color | |
@piece = piece | |
end | |
def compare(otherPiece) | |
if(otherPiece!=nil) | |
return otherPiece.color == @color | |
end | |
return false | |
end | |
def piece | |
@piece | |
end | |
def color | |
@color | |
end | |
end | |
class Game | |
def initialize() | |
@players = 2 | |
@turn = Random.rand(2) | |
@board = Board.new(10,10) | |
@pieces = [Piece.new("red","X"),Piece.new("black","O")] | |
gameLoop() | |
end | |
def clearScreen() | |
puts "\e[H\e[2J" | |
end | |
def getInput() | |
col = "" | |
while(1) | |
puts "\nENTER A COLUMN TO DROP YOUR PIECE: " | |
STDOUT.flush | |
col = gets.chomp | |
if(col.is_i?&&col.to_i>=0) | |
break | |
else | |
puts "\n***ERROR IN INPUT TRY AGAIN***" | |
end | |
end | |
return col.to_i | |
end | |
def gameOver() | |
return @board.boardFull() || @board.fourConnected() | |
end | |
def gameLoop | |
while(!gameOver()) | |
clearScreen() | |
puts "PLAYER #{@turn+1} TURN\n\n" | |
puts @board.drawBoard() | |
if(@board.dropPiece(getInput(),@pieces[@turn])) | |
@turn = (@turn+1)%(@players) | |
else | |
puts "\n***ERROR IN DROPPING PIECE***" | |
end | |
end | |
clearScreen() | |
puts "GAME OVER" | |
puts "PLAYER #{@turn+1} WINS!!!\n\n" | |
puts @board.drawBoard() | |
end | |
end | |
newGame = Game.new() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment