| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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. |
|
#2
| |||
| |||
| 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 |
|
#3
| |||
| |||
| 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). |
|
#4
| |||
| |||
| 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. |
|
#5
| |||
| |||
| 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. |
|
#6
| |||
| |||
| 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? |
|
#7
| |||
| |||
| 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. |
|
#8
| |||
| |||
| 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. |
|
#9
| |||
| |||
| 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. |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.