Can someone help with my connect 4 project
Author |
Message |
customae7
|
Posted: Thu Mar 22, 2007 9:08 am Post subject: Can someone help with my connect 4 project |
|
|
Turing: | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CONNECT 4 %
% ICS 4M %
% March 2007 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
setscreen ("graphics:max;max") %setscreen to full size
setscreen ("offscreenonly") %allows smooth animation
setscreen ("nocursor") %hides cursor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DECLARATION OF CONSTANTS, VARIABLES, AND TYPES %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
const radius : int := 25
const player1 : int := red
const player2 : int := black
const none : int := white
const space : int := 10
type boardCell :
record
x : int %x coordinate of cell
y : int %y coordinate of cell
owner : int %colour of cell
end record
var board : array 1 .. 7, 1 .. 6 of boardCell %sets the size of board
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PROCEDURES AND FUNCTIONS %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
procedure introScreen ()
%precondition: none
%postcondition: intro screen is displayed
end introScreen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
procedure rules ()
%precondition: none
%postcondition: rules have been displayed
end rules
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
procedure initBoard (var board : array 1 .. 7, 1 .. 6 of boardCell )
%precondition: board has been declared
%postcondition: sets postion of cells and starts them all empty
for i : 1 .. 7
for j : 1 .. 6
%the next two lines set the locations of the cells
board (i, j ).x := 185 + (i * (2 * radius + space )) - radius
board (i, j ).y := 115 + (j * (2 * radius + space )) - radius
board (i, j ).owner := none %sets cells to empty
end for
end for
end initBoard
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
procedure drawBoard (board : array 1 .. 7, 1 .. 6 of boardCell )
%precondtion: initBoard had been called
%postcondtion: board has been displayed on output screen
drawbox (185, 115, 615, 485, yellow) %main part of board
for i : 1 .. 7
for j : 1 .. 6
%Outline cells
drawoval (board (i, j ).x, board (i, j ).y, radius, radius, yellow)
end for
locatexy (board (i, 1).x, 100) %displays column numbers below board
put i
end for
drawfill (190, 120, yellow, yellow) %Fills in board yellow
drawfillbox (185, 50, 175, 485, blue) %left leg of board
drawfillbox (615, 50, 625, 485, blue) %right leg of board
drawfillbox (185, 120, 625, 110, blue) %bottom stopper of board
View.Update
end drawBoard
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
procedure drawChip (x, y, owner : int)
%precondtion: dropchip has been called
%postcondition: draws chip at location (x,y)
drawfilloval (x, y, radius, radius, owner )
end drawChip
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function findHeight (column : int) : int
%precondition: board has been initialized, and row has been entered
%postcondition: determines which row chip must fall to
var cellheight : int
for decreasing j : 6 .. 1
%Find which row the chip needs to fall to
end for
result cellheight
end findHeight
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
procedure drawPreviousChips ()
%precondition: board has been initialized
%postcondition: draws previously played chips
for i : 1 .. 7
for j : 1 .. 6
%only draw cells that have chip in them (reduces flashing)
%use an if statement to see what chips must be drawn
end for
end for
View.Update
end drawPreviousChips
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
procedure dropChip (column, owner : int)
%precondition: isEmpty has been called
%postcondition: animates chip falling down board
var columnX : int
columnX := board (column, 1).x %determines x-coordinate of chip
%animates chip dropping
for decreasing y : 500 .. findHeight (column ) by 2
cls
drawChip (columnX, y, owner )
drawBoard (board )
drawPreviousChips ()
View.Update
end for
board (column, row ).owner := owner
end dropChip
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function isWinner (board : array 1 .. 7, 1 .. 6 of boardCell ) : boolean
%precondition: board has been initialized
%postcondition: determines if there is a winner
% Determines if there is a Horizontal winner
% Determines if there is a Vertical winner
% Determines if there is a Diagonal (up to the right) winner
% Determines if there is a Diagonal (up to the left) winner
%no winner
result false
end isWinner
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function isEmpty (row : int) : boolean
%precondition: board has been initialized, and user has entered column
%postcondition: returns true if chip can be played in that row
if board (row, 6).owner = none then %if top cell is open, chip can be played
result true
else
result false %Column is full
end if
end isEmpty
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MAIN PROGRAM %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var cellheight : int %height chip drops to
var turn : int := 0 %keeps track of who's turn it is
var owner : int := 0 %whose chip is being played (colour)
var ownerString : string %whose chip is being played (word)
var count : int %number of chips played
var font1 : int := Font.New ("sans serif:36:bold")
var tempstr : string
introScreen ()
rules ()
%allows to play more than once
loop
size1 := 210
size2 := 210
count := 0
initBoard (board ) %Initialize board
drawBoard (board ) %draws board
%drawStacks (size1, size2)
%loops single game
loop
if turn mod 2 = 0 then %Determines who turn it is
owner := player1 %sets colour of chip
ownerString := "Player 1" %sets name of current player
else
owner := player2 %sets colour of chip
ownerString := "Player 2" %sets name of current player
end if
getch (input ) %getch() do not need to hit enter
if input = "q" or input = "Q" then %allows players to quit game
cls
exit
end if
row := strint (input ) %change input to an integer value
if row >= 1 and row <= 7 then %makes sure move is valid
if isEmpty (row ) then %checks to see if column is empty
dropChip (row, owner ) %animates chip dropping
drawPreviousChips () %draws previously played chips
turn := turn + 1 %changes it to next player
count := count + 1
end if
if isWinner (board ) then %checks for winner
cls
tempstr := ownerString + " WINS!!!!!!"
Font.Draw (tempstr, maxx div 2 - 200, maxy div 2 + 60, font1, red) %displays winner
View.Update
exit %exit out of game loop
elsif count = 42 then %No more cells left
cls
put "TIE GAME"
exit
end if
end if
end loop
Font.Draw ("press any key to continue", maxx div 2 - 300, maxy div 2 - 60, font1, black)
View.Update
getch (input )
cls
Font.Draw ("Do you want to play again (y/n)?", maxx div 2 - 350, maxy div 2 - 60, font1, red)
View.Update
getch (input )
exit when input = "n" or input = "N" %exits out of program
cls
View.Update
end loop
cls
Font.Draw ("Thanks for playing!", maxx div 2 - 250, maxy div 2 - 60, font1, black)
View.Update
|
Freakman Edit: Added Code tags for readability
here is the code but i need a couple things fixed if anyone can help me out |
|
|
|
|
|
Sponsor Sponsor
|
|
|
ZeroPaladn
|
Posted: Thu Mar 22, 2007 9:21 am Post subject: Re: Can someone help with my connect 4 project |
|
|
First things first, Welcome to CompSci.ca!
as for your program, i don't know where to begin. You should probably tell us whats wrong with it in the first place, because most of us don't like reading through code finding problems. Secondly, please use [code] and [/code] tags when posting code, it scales it down and makes it much more legible in the posts.
As for your code, i skimmed through it and couldn't find anything unusual. Then again, im not as good as many others on this site. |
|
|
|
|
|
Clayton
|
Posted: Thu Mar 22, 2007 3:35 pm Post subject: Re: Can someone help with my connect 4 project |
|
|
You're going to have to be more specific. I personally don't feel like looking over a few hundred lines of code to find a problem. What exactly is the problem you're having? I'm on Linux so I can't run your code right now. |
|
|
|
|
|
|
|