Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to write a simple C program?

8 views
Skip to first unread message

Rita Ko

unread,
Oct 27, 2000, 10:57:15 PM10/27/00
to
Hi!

I would like to seek your help to write this program:

"Tic-tac-toe", also known as "noughts-and-crosses" is a game played on a
three-by-three board by two persons. One player plays the nought and the
other, cross. Each player takes it in turns to place their respective
piece on the board that is not occupied by any other pieces.

A player wins when three of his pieces are played in line, either
horizontally, vertically or diagonally. The players draw when neither win,
and there are no empty places on the board.

Implement the game using C which allow a user to play with the computer.
The program must be able to maintain records of the games played.

The file must be implemented for binary access, and the record structure is
as follows:

Player Name of the player - 20 characters + Null Character
Date Using nested structure - comprising dd, mm and yy as integers
Time Using nested structure - comprising hh, mm as integers
Win Number of games won - integer
Lose Number of games lost - integer

The program allows user to do the following task:
1. Play the game
2. Display player statistics
3. Quit

The program will only end when player chooses to quit.

When player chooses to display statistics, the following information is to
be displayed:

Name : <current player's name>
Games won : <sum of all the games won (including those stored in the file)>
Games lost : <sum of all the games lost (including those stored in the
file)>

----------------------
Please show and explain to me how to write such a program above.

Thank you in advance for your help.

Regards
Ge Li
email: febr...@singnet.com.sg

Jack Klein

unread,
Oct 27, 2000, 11:13:34 PM10/27/00
to
On Sat, 28 Oct 2000 10:57:15 +0800, "Rita Ko"
<febr...@singnet.com.sg> wrote in comp.lang.c:

See the first article on this web page:

http://home.att.net/~jackklein/ctips01.html

for valuable help.

Jack Klein
--
Home: http://jackklein.home.att.net
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.faqs.org/faqs/C-faq/learn/

Kaz Kylheku

unread,
Oct 27, 2000, 11:15:29 PM10/27/00
to
On Sat, 28 Oct 2000 10:57:15 +0800, Rita Ko <febr...@singnet.com.sg> wrote:
>Hi!
>
>I would like to seek your help to write this program:

Do your own homework.

>Implement the game using C which allow a user to play with the computer.

Game playing algorithms in particular, and AI in general, are off topic in
comp.lang.c.

But here is a hint: tic tac toe is a simple enough game that your program can
play by using brute force; try every possible move and find the one that leads
to victory. It's even feasible to construct a table beforehand which, for each
possible configuration of the game board, simply tells the program what move to
play.

>Please show and explain to me how to write such a program above.

That is your teacher's job. Now if you exert the effort to make a program, and
it contains mistakes, then maybe someone in comp.lang.c can help you remove the
mistakes. You will then have a C program, something that you can actually
discuss in a C programing newsgroup. Right now you have nothing that could be
used as the basis of a C discussion.

Rita Ko

unread,
Oct 28, 2000, 7:51:56 AM10/28/00
to
Hi!

Thanks for the advice. I need to use array and initialise to 0.

I've tried to draw the board first but I do not know how to use the ASCII
Character Chart:
024 = arrow move upwards
025= arrow move downwards
026= arrow move to the right
027= arrow move to the left
----------
My draft of the board program:

#include <stdio.h>
#include <stdlib.h>

int board[3][3] = {1,2,3,4,5,6,7,8,9};
int x, y;

main
{
board[0][0]=1;
board[0][1]=2;
board[0][2]=3;
board[1][0]=4;
board[1][1]=5;
board[1][2]=6;
board[2][0]=7;
board[2][1]=8;
board[2][2]=9;

return 0;
}


Kaz Kylheku <k...@ashi.footprints.net> wrote in message
news:slrn8vkhg...@ashi.FootPrints.net...

al

unread,
Oct 28, 2000, 9:07:42 PM10/28/00
to

"Rita Ko" <febr...@singnet.com.sg> wrote in message
news:8tdfe4$r0r$1...@mango.singnet.com.sg...

your instructor will be impressed by this one.
can't remeber who posted this here originally.

#include<stdio.h>
#define R return
#define P printf
#define F for
#define I if
#define V void
#define K int
#define C char
K bm[20];static K an[8]={0111,0222,0333,07,070,0700,0124,0421};K sr(K pl
,K sm,K a,K b,K p){K i,sc;K bs=-300;F(i=0;i<8;i++)I(((p>>(sm^9))&an[i])
==an[i])R(-12+pl);I((p>>18)==0777)R(0);F(i=(1<<18)|(1<<sm);i<(1<<27);i=i
<<1){I(i&p)continue;sc=-sr(pl+1,sm^9,-b,-a,p|i);I(sc>bs){bs=sc;bm[pl]=i;
}I(sc>=b)break;I(sc>a)a=sc;}R(bs);}V sh(K p){K i;C A[20]="- - -\n- - -\
\n- - -\n";F(i=0;i<18;i++)I((1<<i)&p)A[(i%9)*2]=(i>8)?'O':'X';P("\n\n%s"
,A);}K main(V){K c=9;K p=0;F(;(p>>18)<0777;sh(p|=bm[1]))sr(1,c^=9,-300,
300,p);R 0;}


to write your own
basically work out each part and functions needed

I have cut the comments out of another tic tac toe program.
to give you an idea of a possible way to split the program up.

#include <stdlib.h>
#include <stdio.h>

/* few defines to set things up eg #define WINX 0 , #define WINO 1 #define
DRAW 2
#define UNFINISHED 3 */
#define

/* show the user the playing instructuons */

/* get a move from the player and add it to the board */

/* get the computers move */

/* display the current game board */

/* test to see if anyone has won */
/* test to see if 'O' has won */
/* test to see if 'X' has won */

/* test to see if it is draw - i.e. no spaces left, but no one has won */
/* game must therefore be unfinished */

/* show who won the game */

/* main routine for playig tic-tac-toe */
/* get the name of the player */
/* set the board up with all blanks */
/* keep making moves till game over */
/* get the players move */
/* if the game is finished then dont bother with computers move
*/
/* get the computers move */
/* show who won the game */

Alex

al

unread,
Oct 28, 2000, 9:39:07 PM10/28/00
to

"Rita Ko" <febr...@singnet.com.sg> wrote in message
news:8teeom$ql8$1...@mango.singnet.com.sg...

> Hi!
>
> Thanks for the advice. I need to use array and initialise to 0.
>
> I've tried to draw the board first but I do not know how to use the ASCII
> Character Chart:
> 024 = arrow move upwards
> 025= arrow move downwards
> 026= arrow move to the right
> 027= arrow move to the left
> ----------
> My draft of the board program:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int board[3][3] = {1,2,3,4,5,6,7,8,9};
you sure this is want you want

> int x, y;
>
> main

int main(void)

> {
> board[0][0]=1;
> board[0][1]=2;
> board[0][2]=3;
> board[1][0]=4;
> board[1][1]=5;
> board[1][2]=6;
> board[2][0]=7;
> board[2][1]=8;
> board[2][2]=9;
>
> return 0;
> }

have you considered using a struct to hold the game data

for example
typedef struct
{
char player; /* char array to store players name */
char status; /* stores current status of game */
int board[3][3]; /* board */
} Game_Info;

typedef struct Game_Info info; /* places the data from struct Game_Info to
info */

there is plenty of information on structs in
C:How to program by Deitel and Deitel chapter 10
which seems to be the standard text for intro to c or informatics
at some of universities and colleges

for the player to select where to move
it might be easier to let them press a number key corresponding
to the position
could aslo use an enum (enumeration) to set this

Alex

http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c
http://www.faqs.org/faqs/C-faq/learn/ alt.comp.lang.learn.c-c++

G.Gabriele

unread,
Oct 29, 2000, 11:32:59 AM10/29/00
to
Rita Ko wrote:
>
> Hi!
>
> I would like to seek your help to write this program:
>
> "Tic-tac-toe", also known as "noughts-and-crosses" is a game played on a
> three-by-three board by two persons. One player plays the nought and the
> other, cross. Each player takes it in turns to place their respective
> piece on the board that is not occupied by any other pieces.
>
<snip >

A tic tac toe game from a beginner C programmer ....

http://nettoe.sourceforge.net

--
+ g.gab...@europe.com ---------------+
| |
+------------------ think Open Source -+

Rita Ko

unread,
Nov 6, 2000, 3:00:00 AM11/6/00
to
Hi!

I've tried running this program and I do not know how to record the number
of games won or lost made by the player.

Thanks for the guidance.

Ge Li

#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>

void board(char [3][3]);
int win(char [3][3],char);
int lose(char [3][3],char);
int attack(char board[3][3], char X, char O);
void game(void);
int pmove(char play, char board[3][3],char X);
void pl_stats(void);
void show_stats(void);

typedef struct
{
int da_day;
int da_mon;
int da_year;
}date_d;

typedef struct
{
int ti_hour;
int ti_min;
}time_h;

struct p_stats
{
char name[21];
int won;
int lose;
struct date d;
struct time t;
}stats;

void main(void)
{
char menu;

while(1)
{
clrscr();
gotoxy(25,5);
printf("T I C T A C T O E");
gotoxy(25,7);
printf(" M E N U ");
gotoxy(25,9);
printf("1. Play the game");
gotoxy(25,11);
printf("2. Display player's statistics");
gotoxy(25,13);
printf("3. Quit");
gotoxy(25,17);
printf("Select your choice: ");
scanf("%c",&menu);

switch(menu)
{
case 'p': case 'P': case '1':
{
game();
pl_stats();
break;
}
case 'D': case 'd': case '2':
{
show_stats();
break;
}
case 'Q': case 'q': case '3':
{
exit (0);
}
default:
{
delay(1000);
break;
}
}
}
}

void game(void)
{
int pturn,anyrow,anycol,flag;
char square[3][3] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
char player = 'X', computer = 'O', play;

clrscr();
board(square);
delay(2000);

for(pturn=6;pturn>0;pturn--)
{
clrscr();
board(square);
/***********COMPUTER'S TURN****************/
if (pturn == 6) // first move//
{
gotoxy(28,19);
printf("MY FIRST MOVE IS...\n");
delay(1000);
square[0][0] = computer;
clrscr();
board(square);
}
else if(pturn == 5)//second move
{
if(flag == 0)
{
gotoxy(23,19);
printf("IT'S MY TURN!! \n");
delay(1000);
}
if(square[1][1] == player)
{
square[2][2] = computer;
}
else if(square[2][2] == player || square[2][0] == player || square[1][2]
== player || square[1][0] == player)
{
square[0][2] = computer;
}
else if(square[0][1] == player || square[0][2] == player || square[2][1]
== player)
{
square[2][0] = computer;
}

clrscr();
board(square);
}
else if (flag == 0)
{
gotoxy(30,19);
printf("OK! MY TURN NOW!\n");
delay(1000);
if (attack(square,computer,computer) == 1)
{
clrscr();
board(square);
if (win(square,computer) == -1)
{
pturn = 0;
getch();
break;
}
getch();
}
else if (attack(square,player,computer) == 1)
{
clrscr();
board(square);
if (win(square,computer) == -1)
{
pturn = 0;
getch();
break;
}
}
else if (square[1][1] == computer ||
square[1][0] == player && square[2][1] == player && square[0][2] == '
')
{
square[0][2] = computer;
}
else if (square[0][1] == player && square[1][2] == player && square[2][0]
== ' ' ||
square[0][1] == player && square[2][2] == player && square[2][0] == '
')
{
square[2][0] = computer;
}
else if (square[0][1] == player && square[1][0] == player && square[2][2]
== ' ' ||
square[0][1] == player && square[2][0] == player && square[2][2] == ' '
||
square[1][0] == player && square[0][2] == player && square[2][2] == '
')
{
square[2][2] = computer;
}
if (win(square,player) == -1)
{
pturn = 0;
getch();
break;
}
clrscr();
board(square);
}
/***************PLAYER'S TURN*********************/
gotoxy(18,19);
printf("Visualise your number pad as the game board");
gotoxy(26,21);
printf("I play O and you play X");
play = getche();

flag = 0;

flag = pmove(play,square,player);

if (flag != 0)
{
gotoxy(23,19);
printf("Oops! Oh! Oh! ERROR! ENTER TO CONTINUE...\n");
getch();
pturn++;
}

if (win(square,player) == -1)
{
pturn = 0;
getch();
break;
}
clrscr();
board(square);
delay(1000);

}
clrscr();
board(square);
getch();
}

void board(char board[3][3])
{
gotoxy(25,5);
printf("ÉÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍÑÍÍÍÍÍÍÍ»");
gotoxy(25,6);
printf("º ³ ³ º");
gotoxy(25,7);
printf("º %c ³ %c ³ %c º",board[0][0],board[0][1],board[0][2]);
gotoxy(25,8);
printf("º ³ ³ º");
gotoxy(25,9);
printf("ÇÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄĶ");
gotoxy(25,10);
printf("º ³ ³ º");
gotoxy(25,11);
printf("º %c ³ %c ³ %c º",board[1][0],board[1][1],board[1][2]);
gotoxy(25,12);
printf("º ³ ³ º");
gotoxy(25,13);
printf("ÇÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄĶ");
gotoxy(25,14);
printf("º ³ ³ º");
gotoxy(25,15);
printf("º %c ³ %c ³ %c º",board[2][0],board[2][1],board[2][2]);
gotoxy(25,16);
printf("º ³ ³ º");
gotoxy(25,17);
printf("ÈÍÍÍÍÍÍÍÏÍÍÍÍÍÍÍÏÍÍÍÍÍÍͼ");
}

int win(char board[3][3], char X)
{

if ((board[0][0] == X && board[0][1] == X && board[0][2] == X) ||
(board[1][0] == X && board[1][1] == X && board[1][2] == X) ||
(board[2][0] == X && board[2][1] == X && board[2][2] == X) ||
(board[0][0] == X && board[1][0] == X && board[2][0] == X) ||
(board[0][1] == X && board[1][1] == X && board[2][1] == X) ||
(board[0][2] == X && board[1][2] == X && board[2][2] == X) ||
(board[0][0] == X && board[1][1] == X && board[2][2] == X) ||
(board[0][2] == X && board[1][1] == X && board[2][0] == X))
{
if (X == 'X')
{
gotoxy(25,19);
printf("Phew! U Won!\n");
return -1;
}
else
{
gotoxy(27,19);
printf("Hehehe! U Lose! Try Again!\n");
return -1;
}
}
else if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' &&
board[1][0] != ' ' && board[1][1] != ' ' && board[1][2] != ' ' &&
board[2][0] != ' ' && board[2][1] != ' ' && board[2][2] != ' ')
{
gotoxy(21,19);
printf("It's a Draw!!\n");
return -1;
}
return 0;
}

int attack(char board[3][3], char X, char O)
{
int i;

for(i=0;i<3;i++)
{
if(board[i][0] == X && board[i][1] == X && board[i][2] == ' ')
{
board[i][2] = O;
return 1;
}
else if(board[i][1] == X && board[i][2] == X && board[i][0] == ' ')
{
board[i][0] = O;
return 1;
}
else if(board[0][i] == X && board[1][i] == X && board[2][i] == ' ')
{
board[2][i] = O;
return 1;
}
else if(board[1][i] == X && board[2][i] == X && board[0][i] == ' ')
{
board[0][i] = O;
return 1;
}
else if(board[i][0] == X && board[i][2] == X && board[i][1] == ' ')
{
board[i][1] = O;
return 1;
}
else if(board[0][i] == X && board[2][i] == X && board[1][i] == ' ')
{
board[1][i] = O;
return 1;
}
}

if((board[0][0] == X && board[2][2] == X && board[1][1] == ' ') ||
(board[0][2] == X && board[2][0] == X && board[1][1] == ' '))
{
board[1][1] = O;
return 1;
}
else if(board[0][0] == X && board[1][1] == X && board[2][2] == ' ')
{
board[2][2] = O;
return 1;
}
else if(board[1][1] == X && board[2][2] == X && board[0][0] == ' ')
{
board[0][0] = O;
return 1;
}
else if(board[1][1] == X && board[0][2] == X && board[2][0] == ' ')
{
board[2][0] = O;
return 1;
}
else if(board[2][0] == X && board[1][1] == X && board[0][2] == ' ')
{
board[0][2] = O;
return 1;
}
else
{
return 0;
}

}

int pmove(char play, char board[3][3], char X)
{
if (play == '7' && board[0][0] == ' ')
{
board[0][0] = X;
}
else if (play == '8' && board[0][1] == ' ')
{
board[0][1] = X;
}
else if (play == '9' && board[0][2] == ' ')
{
board[0][2] = X;
}
else if (play == '4' && board[1][0] == ' ')
{
board[1][0] = X;
}
else if (play == '5' && board[1][1] == ' ')
{
board[1][1] = X;
}
else if (play == '6' && board[1][2] == ' ')
{
board[1][2] = X;
}
else if (play == '1' && board[2][0] == ' ')
{
board[2][0] = X;
}
else if (play == '2' && board[2][1] == ' ')
{
board[2][1] = X;
}
else if (play == '3' && board[2][2] == ' ')
{
board[2][2] = X;
}
else
{
return 1;
}
return 0;
}

void pl_stats(void)
{
struct p_stats stats;
FILE *fp;

clrscr();
char name[21];
printf("Enter your name: ");
gets(name);
// printf("Games won: %d\n", int *won + stats[c].win);
// printf("Games lost: %d\n",int *lose + stats[c].lose);
// };
fflush(fp);
scanf("%s",stats.name);


struct date d;
getdate (&stats.d);
printf("%d %d %d\n",d.da_day, d.da_mon, d.da_year);
struct time t;
gettime (&stats.t);
printf("%2d:%02d\n",t.ti_hour, t.ti_min);

if((fp=fopen("tic.txt","a"))==NULL)
{
printf("\aCreating records for intitial storage...\n");
getch();
}

fprintf(fp,"%s\n",stats.name);
fprintf(fp,"%d/%d/%d\n",stats.d.da_day,stats.d.da_mon,stats.d.da_year);
fprintf(fp,"%2d:%02d\n",stats.t.ti_hour, stats.t.ti_min);

fclose(fp);
getch();
}

void show_stats(void)
{
struct p_stats stats;
FILE *fp;

if((fp=fopen("tic.txt","rt"))==NULL)
{
printf("\aNo records to display...\n");
getch();
}

clrscr();

while(!feof(fp))
{
fgets(stats.name,21,fp);
fscanf(fp,"%d/%d/%d",&stats.d.da_day,&stats.d.da_mon,&stats.d.da_year);
fscanf(fp,"%2d:%02d\n",&stats.t.ti_hour,&stats.t.ti_min);
printf("\nName: %sDate:
%d/%d/%d",stats.name,stats.d.da_day,stats.d.da_mon,stats.d.da_year);
printf("\nTime: %2d:%02d\n\n",stats.t.ti_hour,stats.t.ti_min);
}

fclose(fp);
getch();
}

0 new messages