Connect Four game - 2D Array problem?

This is a discussion on Connect Four game - 2D Array problem? within the Pascal forums in Programming Languages category; Hi all. I have been writing a connect four game. So far I have defined the board using a 2D Array. Because I have no graphical programming experience, this is being done in the console application. My board is displayed and in each cell appears notation which represents that cell. The user chooses for e.g. 'A1'. So the counter appears in this cell. That's how I want it to go, I've tried implementing it using a search algorithm, but something is up and I'm sturuggling with this, help would be very appreciated, many thanks. PROGRAM ConnectFour; {$APPTYPE CONSOLE} uses SysUtils; ...

Go Back   Application Development Forum > Programming Languages > Pascal

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 01-18-2008, 08:25 PM
israphelr@googlemail.com
Guest
 
Default Connect Four game - 2D Array problem?

Hi all.

I have been writing a connect four game.

So far I have defined the board using a 2D Array.

Because I have no graphical programming experience, this is being done
in the console application.

My board is displayed and in each cell appears notation which
represents that cell. The user chooses for e.g. 'A1'. So the counter
appears in this cell. That's how I want it to go, I've tried
implementing it using a search algorithm, but something is up and I'm
sturuggling with this, help would be very appreciated, many thanks.

PROGRAM ConnectFour;

{$APPTYPE CONSOLE}

uses
SysUtils;

TYPE
Tboard = ARRAY[65..71,49..55] OF STRING;


{*************************************************
**********************
Creates the Connect four board
**************************************************
**********************}
PROCEDURE makeBoard(VAR board : Tboard);

VAR
x, y : integer;
m, n : STRING;

BEGIN
FOR x:= 65 TO 71 DO
BEGIN
m := chr(x);
FOR y := 49 TO 55 DO
BEGIN
n := chr(y);
board[X,Y] := m + n;
END;
END;
END;



{*************************************************
***********************
Accesses the board and displays it.
**************************************************
***********************}
PROCEDURE displayBoard(board : Tboard);

VAR
X, Y : INTEGER;

BEGIN
writeln('
----------------------------------------------------------------');
FOR x:= 65 TO 71 DO
BEGIN
FOR y := 49 TO 55 DO
BEGIN
write(' |');
write(' ',board[X,Y]);
END;
write(' |');
writeln;
writeln(' ==================================================
==============');
END;
END;

PROCEDURE placeCounter(cell : STRING; var board : Tboard);

VAR
x, y : integer;

BEGIN
FOR x:= 65 TO 71 DO
FOR y:= 49 TO 55 DO
IF (board[x,y] = 'A1') THEN
board[x,y] := '#'
END;


PROCEDURE chooseCell(board : Tboard);

VAR
cell : STRING;

BEGIN
writeln;
writeln('Choose a cell you would like to drop a counter in.(e.g.
''B3'') : ');
readln(cell);
END;


{*************************************************
****************
Puts all of the procedures uses in the game together, The loop
controll will also be administered in here(overall game control).
**************************************************
*****************}
PROCEDURE newGame();

VAR
board : Tboard;
won : boolean;

BEGIN
won := false;


makeboard(board);
repeat
displayboard(board);
chooseCell(board);
until (won = true);

END;


PLEASE NOTE HERE : I have attempted to replace 'A1' string with '#' to
represent the cell. Ultimately this is not how the program will work,
but at the moment my primary concern is getting this search and
replace procedure working.
Reply With Quote
  #2  
Old 01-19-2008, 10:51 AM
Wolf Behrenhoff
Guest
 
Default Re: Connect Four game - 2D Array problem?

israphelr@googlemail.com schrieb:
> Hi all.
>
> I have been writing a connect four game.
>
> So far I have defined the board using a 2D Array.
>
> Because I have no graphical programming experience, this is being done
> in the console application.
>
> My board is displayed and in each cell appears notation which
> represents that cell. The user chooses for e.g. 'A1'. So the counter
> appears in this cell. That's how I want it to go, I've tried
> implementing it using a search algorithm, but something is up and I'm
> sturuggling with this, help would be very appreciated, many thanks.


The user only needs to enter a column!

>
> PROGRAM ConnectFour;
>
> {$APPTYPE CONSOLE}
>
> uses
> SysUtils;
>
> TYPE
> Tboard = ARRAY[65..71,49..55] OF STRING;


What is that?

The board has 7 cols and 6 rows. So the easiest way is
TBoard = Array[0..6, 0..5] of Whatever;

Why an array of string? Better define a type of what can be at that
position. For example
type TField = (empty, red, yellow);

There is no need to store something that you already know! You know that
the file in the lower left corner is A1, so why store that?

You should NOT define internal structures (like the TBoard) based on a
representation the user sees. Rather define it so that they match the
problem.

And more, you should put functions/procedures and the internal board
representation together in a class.

What compiler are you using? (I'm asking because of the $APPTYPE)

You could do (in Delphi, FPC)
type TBoard = class
private
fBoard: Array...

...
public
procedure Draw;
function getCell(x,y: Integer): TField;
function canPut(ACol: Integer): Boolean;
procedure Put(Acol: Integer);
constructor Create(who: TWhoStarts);
...
end


Wolf
Reply With Quote
  #3  
Old 01-19-2008, 05:44 PM
israphelr@googlemail.com
Guest
 
Default Re: Connect Four game - 2D Array problem?

On Jan 19, 7:51*am, Wolf Behrenhoff
<NoSpamPleaseButThisIsVal...@gmx.net> wrote:
> israph...@googlemail.com schrieb:
>
> > Hi all.

>
> > I have been writing a connect four game.

>
> > So far I have defined the board using a 2D Array.

>
> > Because I have no graphical programming experience, this is being done
> > in the console application.

>
> > My board is displayed and in each cell appears notation which
> > represents that cell. The user chooses for e.g. 'A1'. So the counter
> > appears in this cell. That's how I want it to go, I've tried
> > implementing it using a search algorithm, but something is up and I'm
> > sturuggling with this, help would be very appreciated, many thanks.

>
> The user only needs to enter a column!
>
>
>
> > PROGRAM ConnectFour;

>
> > {$APPTYPE CONSOLE}

>
> > uses
> > * SysUtils;

>
> > TYPE
> > * Tboard = ARRAY[65..71,49..55] OF STRING;

>
> What is that?
>
> The board has 7 cols and 6 rows. So the easiest way is
> TBoard = Array[0..6, 0..5] of Whatever;
>
> Why an array of string? Better define a type of what can be at that
> position. For example
> type TField = (empty, red, yellow);
>
> There is no need to store something that you already know! You know that
> the file in the lower left corner is A1, so why store that?
>
> You should NOT define internal structures (like the TBoard) based on a
> representation the user sees. Rather define it so that they match the
> problem.
>
> And more, you should put functions/procedures and the internal board
> representation together in a class.
>
> What compiler are you using? (I'm asking because of the $APPTYPE)
>
> You could do (in Delphi, FPC)
> type TBoard = class
> * private
> * * fBoard: Array...
>
> * * ...
> * public
> * * procedure Draw;
> * * function getCell(x,y: Integer): TField;
> * * function canPut(ACol: Integer): Boolean;
> * * procedure Put(Acol: Integer);
> * * constructor Create(who: TWhoStarts);
> * * ...
> * end
>
> Wolf


Thanks for the reply and the help.

I am using Borland Delphi 7

I am Not sure about classes. (Am new to programming)



I'm a little confused here with some things. I am new to this thank
for your patience.

> There is no need to store something that you already know! You know that
> the file in the lower left corner is A1, so why store that?
>
> You should NOT define internal structures (like the TBoard) based on a
> representation the user sees. Rather define it so that they match the
> problem.


From this I am stuck as to how I can represent each square to the
user. Do you actually mean instead of having the cells showing 'A1'
just maybe something like this..
_ _ _
A |_|_|_|
B |_|_|_|
C |_|_|_|
D |_|_|_|

1 2 3

Displayed? (In order to obtain player input).
Reply With Quote
  #4  
Old 01-19-2008, 05:49 PM
israphelr@googlemail.com
Guest
 
Default Re: Connect Four game - 2D Array problem?

On Jan 19, 7:51*am, Wolf Behrenhoff
<NoSpamPleaseButThisIsVal...@gmx.net> wrote:
> israph...@googlemail.com schrieb:
>
> > Hi all.

>
> > I have been writing a connect four game.

>
> > So far I have defined the board using a 2D Array.

>
> > Because I have no graphical programming experience, this is being done
> > in the console application.

>
> > My board is displayed and in each cell appears notation which
> > represents that cell. The user chooses for e.g. 'A1'. So the counter
> > appears in this cell. That's how I want it to go, I've tried
> > implementing it using a search algorithm, but something is up and I'm
> > sturuggling with this, help would be very appreciated, many thanks.

>
> The user only needs to enter a column!
>
>
>
> > PROGRAM ConnectFour;

>
> > {$APPTYPE CONSOLE}

>
> > uses
> > * SysUtils;

>
> > TYPE
> > * Tboard = ARRAY[65..71,49..55] OF STRING;

>
> What is that?
>
> The board has 7 cols and 6 rows. So the easiest way is
> TBoard = Array[0..6, 0..5] of Whatever;
>
> Why an array of string? Better define a type of what can be at that
> position. For example
> type TField = (empty, red, yellow);
>
> There is no need to store something that you already know! You know that
> the file in the lower left corner is A1, so why store that?
>
> You should NOT define internal structures (like the TBoard) based on a
> representation the user sees. Rather define it so that they match the
> problem.
>
> And more, you should put functions/procedures and the internal board
> representation together in a class.
>
> What compiler are you using? (I'm asking because of the $APPTYPE)
>
> You could do (in Delphi, FPC)
> type TBoard = class
> * private
> * * fBoard: Array...
>
> * * ...
> * public
> * * procedure Draw;
> * * function getCell(x,y: Integer): TField;
> * * function canPut(ACol: Integer): Boolean;
> * * procedure Put(Acol: Integer);
> * * constructor Create(who: TWhoStarts);
> * * ...
> * end
>
> Wolf


Thanks for the reply and the help.

I am using Borland Delphi 7

I am Not sure about classes. (Am new to programming)

I'm a little confused here with some things. I am new to this thank
for your patience.

> There is no need to store something that you already know! You know that
> the file in the lower left corner is A1, so why store that?


> You should NOT define internal structures (like the TBoard) based on a
> representation the user sees. Rather define it so that they match the
> problem.


From this I am stuck as to how I can represent each square to the
user. Do you actually mean instead of having the cells showing 'A1'
just maybe something like this..
_ _ _
|_|_|_|
|_|_|_|
|_|_|_|
|_|_|_|

1 2 3

Displayed? (In order to obtain player input).

>The user only needs to enter a column!


Also by this image and your comment, this means I don't have to worry
about 'A1' really right? For users.
Reply With Quote
  #5  
Old 01-19-2008, 05:49 PM
Dr J R Stockton
Guest
 
Default Re: Connect Four game - 2D Array problem?

In comp.lang.pascal.borland message <8fb32f06-d319-4fd6-adea-dedad633d5f
e@l32g2000hse.googlegroups.com>, Fri, 18 Jan 2008 17:25:06,
"israphelr@googlemail.com" <israphelr@googlemail.com> posted:
>
>I have been writing a connect four game.


My eighteen-year-old one is via
<URL:http://www.merlyn.demon.co.uk/games/00index.htm>.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.
Reply With Quote
  #6  
Old 01-19-2008, 07:48 PM
israphelr@googlemail.com
Guest
 
Default Re: Connect Four game - 2D Array problem?

On Jan 19, 2:49*pm, Dr J R Stockton <j...@merlyn.demon.co.uk> wrote:
> In comp.lang.pascal.borland message <8fb32f06-d319-4fd6-adea-dedad633d5f
> e...@l32g2000hse.googlegroups.com>, Fri, 18 Jan 2008 17:25:06,
> "israph...@googlemail.com" <israph...@googlemail.com> posted:
>
>
>
> >I have been writing a connect four game.

>
> My eighteen-year-old one is via
> * <URL:http://www.merlyn.demon.co.uk/games/00index.htm>.
>
> --
> *(c) John Stockton, Surrey, UK. * ?...@merlyn.demon.co.uk * DOS 3.3,6.20; WinXP.
> *Web *<URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
> *PAS EXE TXT ZIP via *<URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
> *My DOS *<URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.


Thanks that's a good version you have there, do you remember how much
programming experience you'd had up until the point where you began
writing that?
Reply With Quote
  #7  
Old 01-20-2008, 01:44 PM
Dr J R Stockton
Guest
 
Default Re: Connect Four game - 2D Array problem?

In comp.lang.pascal.borland message <e1db40d2-616c-47b1-a97e-98bb337199d
2@e4g2000hsg.googlegroups.com>, Sat, 19 Jan 2008 14:44:11,
"israphelr@googlemail.com" <israphelr@googlemail.com> posted:
>
>I am using Borland Delphi 7
>


Then you should be using a newsgroup with Delphi in its name. Read
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt>, which is occasionally
posted here, and <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip>.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
Reply With Quote
  #8  
Old 01-20-2008, 04:04 PM
Dr J R Stockton
Guest
 
Default Re: Connect Four game - 2D Array problem?

In comp.lang.pascal.borland message <14ac749a-cf3a-4cb6-9c24-913d600dcba
5@h11g2000prf.googlegroups.com>, Sat, 19 Jan 2008 16:48:09,
"israphelr@googlemail.com" <israphelr@googlemail.com> posted:
>On Jan 19, 2:49*pm, Dr J R Stockton <j...@merlyn.demon.co.uk> wrote:
>> In comp.lang.pascal.borland message <8fb32f06-d319-4fd6-adea-dedad633d5f
>> e...@l32g2000hse.googlegroups.com>, Fri, 18 Jan 2008 17:25:06,
>> "israph...@googlemail.com" <israph...@googlemail.com> posted:
>>
>>
>>
>> >I have been writing a connect four game.

>>
>> My eighteen-year-old one is via
>> * <URL:http://www.merlyn.demon.co.uk/games/00index.htm>.


>Thanks that's a good version you have there, do you remember how much
>programming experience you'd had up until the point where you began
>writing that?


Yes.

It was written in TP4 or TP5, on an Amstrad PPC640 portable with 2*720kB
floppies and no HDD, possibly with the LCD screen but maybe with an
added CRT. It may have been written in a cold and noisy hut.

The machine still works; I've just tried it.

Please trim quotes, and don't quote signatures.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
Reply With Quote
  #9  
Old 01-20-2008, 04:55 PM
israphelr@googlemail.com
Guest
 
Default Re: Connect Four game - 2D Array problem?



Dr J R Stockton wrote:
> In comp.lang.pascal.borland message <e1db40d2-616c-47b1-a97e-98bb337199d
> 2@e4g2000hsg.googlegroups.com>, Sat, 19 Jan 2008 14:44:11,
> "israphelr@googlemail.com" <israphelr@googlemail.com> posted:
> >
> >I am using Borland Delphi 7
> >

>
> Then you should be using a newsgroup with Delphi in its name. Read
> <URL:http://www.merlyn.demon.co.uk/clpb-faq.txt>, which is occasionally
> posted here, and <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip>.
>
> --
> (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
> <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
> <URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
> <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.


Thanks.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 11:56 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.