# Connect 4 Game - 2D Array search 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.

[code]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'') : ');
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;[/code]

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.

• : 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.
:
: [code]: 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'') : ');
: 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;[/code]:
:
: 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.
:
You can code the placement of the # in a much easier way:

A string is nothing more than an array[1..255] of char. Knowing this you can split the user input into 2 parts (cell[1] and cell[2]). It is then possible to type-cast each of those chars into bytes: byte(cell[1]). This gives you directly the location at which the user wants to place the #.
Now all that's left is to check if that space of the board is unoccupied and perform the replacement.
• : : 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.
: :
: : [code]: : 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'') : ');
: : 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;[/code]: :
: :
: : 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.
: :
: You can code the placement of the # in a much easier way:
:
: A string is nothing more than an array[1..255] of char. Knowing this
: you can split the user input into 2 parts (cell[1] and cell[2]). It
: is then possible to type-cast each of those chars into bytes:
: byte(cell[1]). This gives you directly the location at which the
: user wants to place the #.
: Now all that's left is to check if that space of the board is
: unoccupied and perform the replacement.

Thanks very much for that! (Working now)