[size=4][color=Blue][b]Here is my code for pascal game that I am making.
My problem is that when I enter X and then If I enter the same number again it will overwrite the X, so can someone please find me a solution .. And my another problem is that how I do make human vs. computer mode? CAN SOMEONE TRY TO MAKE MY CODE MORE EFFICIENT PLEASE [/b][/size]
[/color]
[code]
uses crt;
var
inp1,inp2,inp3:char;
i:integer;
count:integer;
begin
clrscr;
textcolor(13);
writeln;
writeln;
writeln;
Writeln(' Tic Tic Toe ');
writeln;
textcolor(yellow);
writeln;
writeln(' | | ');
writeln(' | | ');
writeln(' | | ');
writeln(' 1 |2 |3 ');
writeln(' ------+------+------');
writeln(' | | ');
writeln(' | | ');
writeln(' | | ');
writeln(' 4 |5 |6 ');
writeln(' ------+------+------ ');
writeln(' | | ');
writeln(' | | ');
writeln(' | | ');
writeln(' 7 |8 |9 ');
writeln;
textcolor(12);
writeln;
writeln;
writeln;
writeln(' Player 1 goes first .... P1 is X and P2 is O');
repeat
gotoxy(2,10);
write('Player 1: ');
readln(inp1);
if inp1= '1' then
begin
gotoxy(37,9);
textcolor(42);
writeln('X');
end
else if inp1= '2' then
begin
gotoxy(44,9);
textcolor(42);
writeln('X');
end
else if inp1= '3' then
begin
gotoxy(51,9);
textcolor(42);
writeln('X');
end
else if inp1= '4' then
begin
gotoxy(37,14);
textcolor(42);
writeln('X');
end
else if inp1= '5' then
begin
gotoxy(44,14);
textcolor(42);
writeln('X');
end
else if inp1='6' then
begin
gotoxy(51,14);
textcolor(42);
writeln('X');
end
else if inp1='7' then
begin
gotoxy(37,18);
textcolor(42);
writeln('X');
end
else if inp1='8' then
begin
gotoxy(44,18);
textcolor(42);
writeln('X');
end
else if inp1='9' then
begin
gotoxy(51,19);
textcolor(42);
writeln('X');
end;
{-------------------------Seperates the O--------------------------------}
gotoxy(2,16);
writeln('PLAYER 2: ');
read(inp2);
if inp2= '1' then
begin
gotoxy(37,9);
textcolor(11);
writeln('O');
end
else if inp2= '2' then
begin
gotoxy(44,9);
textcolor(11);
writeln('O');
end
else if inp2= '3' then
begin
gotoxy(51,9);
textcolor(11);
writeln('O');
end
else if inp2= '4' then
begin
gotoxy(37,14);
textcolor(11);
writeln('O');
end
else if inp2= '5' then
begin
gotoxy(44,14);
textcolor(11);
writeln('O');
end
else if inp2='6' then
begin
gotoxy(51,14);
textcolor(11);
writeln('O');
end
else if inp2='7' then
begin
gotoxy(37,18);
textcolor(11);
writeln('O');
end
else if inp2='8' then
begin
gotoxy(44,18);
textcolor(11);
writeln('O');
end
else if inp2='9' then
begin
gotoxy(51,19);
textcolor(11);
writeln('O');
end;
count:=count+1;
until count=9;
readln;
end.[/code]
Comments
- try to break down your code into procedures and functions, don't have one big main block, will bite ya when your code grows, beside saves a lot of typing
- I'd use an array to hold the field status, then a procedure to draw it when a change is made. This way you can avoid double marks, check win/loose situations, apply game logic etc.
- use readkey instead of readln for input, - or better - have cursor or mouse control, increases playability
- implement a quick exit, players will be thankful for that
Also, i have only little knowledge about the procedures.[/size][/color]
Also, i have only little knowledge about the procedures.[/size][/color]
: do the thing were player can exit whenever they want too by pressing
: a key.
Have the main loop exit control linked to a certain keypress ( like ESC for example ). Read my earlier posts, most of the progs feature this.
: Also, i have only little knowledge about the
: procedures.
:
Read through some pascal tutorials ( there are a bunch of them on the net ) to get the hang of the basics first...
This is a full tic tac toe game in pascal, maybe it will help
[code]
VAR
{1D Array}
Board : Array[1..9] Of Char;
{2D Array}
Board : Array[1..3,1..3] Of Char; { 3x3 = 9 }
[/code]
Either way works well. For the examples, I am using box 5:
With a 1D array it's easy to select the box that the user chooses, as Spot 5 on the board = Board[[b]5[/b]].
With a 2D array Spot 5 on the board = Board[[b]1,2[/b]] or Board[5 DIV 3, 5 MOD 3].
But for drawing the X or O onto the board, a 2D array is setup the same as the board, so:
[code]
{ 1D array }
for y := 1 to 3 do
for x := 1 to 3 do
begin
gotoXY(x*3,y*3);
write(Board[[b](y-1)*3 + x[/b]]);
end;
{ 2D array }
for y := 1 to 3 do
for x := 1 to 3 do
begin
gotoXY(x*3,y*3);
write(Board[[b]x,y[/b]]);
end;
[/code]
Either way has its advantages and disadvantages, but both work basically the same. (Oh, and now you have some needed code too