Friday, September 4, 2009

2009 Engineering Computing Assignment Ver2

Questions:

Question 1: Tic-Tac-Toe

For this question, you are going to use two-dimensional array concepts to program a game called Tic-Tac-Toe. You should set up the program so that a human can play against another human (two people). Tic-Tac-Toe is a board game with a 3 x 3 board of squares, all of which are initially empty. There are two players, known as ‘X’ and ‘O’, who take turns making moves. In this version of the game, the ‘X’ player will always move first. On a player’s turn, the player must place exactly one piece on an empty square of the board. The ‘X’ player always places pieces labeled ‘X’ and the ‘O’ player always places pieces labeled ‘O’. The game ends when either player fills three consecutive positions of the board, in a straight line that is either vertical, horizontal or diagonal. The player to first accomplish this wins. If the whole board fills with neither player able to get three of his or her pieces in a row, then the game is a draw.

Your program should begin by asking the name for player ‘X’ and player ‘O’. Then, your program should draw an empty Tic-Tac-Toe board on the screen. After that, your program should prompt the `X' player to enter a move, and redraw the board with the newly placed piece in the correct position. After every move, the program should test to see if the player won or the board is filled up, in which case the game is over and the program can print the winner (or that it is a draw).

You may assume that when the user is asked to enter a row and a column to place a piece, then the user always enters two integers. However, you should not assume that the user enters integers that correspond to valid board positions - the player may pick a spot that corresponds to a spot of the edges of the board, or a spot that is already occupied. In this case, the program should keep prompting the user for a new spot until the he or she enters a valid position (Your program should perform any necessary error checking).

The following is a sample run of one version of the choice. Your program should behave in a similar manner, but it does not have to be identical to this one.

Sample Output
.................................................
. TIC-TAC-TOE.
.................................................
Welcome to the game of tic-tac-toe!
Player1, what is your name? Shereen
Player2, what is your name? Chadd
0 1 2
--- --- ---
0 | | | |
--- --- ---
1 | | | |
--- --- ---
2 | | | |
--- --- ---

Shereen, please make your move:
Enter row: 0
Enter column: 0
0 1 2
--- --- ---
0 | X | | |
--- --- ---
1 | | | |
--- --- ---
2 | | | |
--- --- ---

Chadd, please make your move:
Enter row: 1
Enter column: 1
0 1 2
--- --- ---
0 | X | | |
--- --- ---
1 | | O | |
--- --- ---
2 | | | |
--- --- ---



Question 2: Laundry Management System

CLEAN LAUNDRY is a new laundry centre near UTAR University. As business is growing rapidly, they are having problems keeping track of their customers and their daily business transactions. You are asked to develop a simple laundry management system to cater for their business. Assume that there are maximum 40 customers per day.

The program will first read the customer information (name, age and sex), weight of the laundry and calculate the total payment for that customer. The price per kilogram for the laundry is based on the table below.

Weight| Price per kilogram
Less than 3kg| RM1.20
3kg to 6kg| RM1.00
More than 6kg| RM0.80

The program will then print a billing receipt that contains the following information:
• The centre information (name, address, and telephone number),
• Customer’s information.
• Weight of the laundry
• Price of kilogram
• Payment
Use structure to store the above information. You should include as well typedef and structure within structure whichever applicable.

Ask the user if they would like to continue (“STOP” for stop, “CONTINUE” for continue). When the choice is “STOP”, the program will display the summary of the day contains total number of customers for the day and the total payment for the day.


Answer:

* This tic-tac-toe program allow 2 human player to play againts each *
* other with player X always starts 1st. After each game, players will *
* be prompt if to play again. *
* All user input were taken as string to check for error and later *
* convert to a proper format for calculationa or storage. *
* String for input were given plenty of memory space to prevent *
* gets() function from giving error when operator input longer *
* than assigned memory space. *
**************************************************************************/

//preprocessors directives
#include
#include
#include
#include

//functions declaration
void welcome();
void name_X();
void name_O();
void print_table();
void won(char);
void players(char);
int winnercheck(char);
int check_draw();
int continues();


//global variables declaration
char nameX[100],nameO[100];
int checkX[3][3];
int checkO[3][3];

void main()
{
char con;
int i,j;
do{

//to initialize the array to 0
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
checkX[i][j]=0;
checkO[i][j]=0;
}
}

welcome();//call func print welcome screen
name_X();//cal func prompt name of X
name_O();//call func prompt name of O

players('x');

con=continues();//call func prompt user to play again?
}while(con==1);

}




void welcome()
{//clear screen and display welcome screen
system("cls");
printf("#################################################\n");
printf("#\t\t\t\t\t\t#\n");
printf("#\t\t TIC-TAC-TOE\t\t\t#\n");
printf("#\t\t\t\t\t\t#\n");
printf("#################################################\n");
printf("\tWelcome to the game of tic-tac-toe!\n\n");
}



void name_X()
{
int i;

do{//repeat while input is empty
printf("Player1, what is your name? ");
gets(nameX);//get player X name

if(strlen(nameX)==0)//check if input is empty
{
printf("Invaid Input!\n");
system("pause");
}
}while(strlen(nameX)==0);

//make 1st char of each word to uppercase
nameX[0]=toupper(nameX[0]);
for(i=0;i<(strlen(nameX));i++)
if(isspace(nameX[i]))
nameX[i+1]=toupper(nameX[i+1]);
return;
}
void name_O()
{
int i;

do{//repeat while input is empty
printf("Player2, what is your name? ");
gets(nameO);//get player O name

if(strlen(nameO)==0)//check if input is empty
{
printf("Invaid Input!\n");
system("pause");
}
}while(strlen(nameO)==0);

//make 1st char of every word to uppercase
nameO[0]=toupper(nameO[0]);
for(i=0;i<(strlen(nameO));i++)
if(isspace(nameO[i]))
nameO[i+1]=toupper(nameO[i+1]);
return;
}

void print_table()
{
int i=0,j=0;
system("cls");//clear screen
printf("\n\n");
//start printing table with correct marking
printf("\t 0 1 2\n");
printf("\t%c---%c---%c---%c\n",218,194,194,191);

for(i=0;i<3;i++)
{
printf(" %d |",i);
for(j=0;j<3;j++)
{
if(checkX[i][j]==1)
printf(" X |");
else
if(checkO[i][j]==1)
printf(" O |");
else
printf(" |");
}
printf("\n\t%c---%c---%c---%c\n",(i==2?192:195),(i==2?193:197),(i==2?193:197),(i==2?217:180));
}
return;
}


int continues()
{
char con[10];
int cont;
do{
printf("\nPlay again?\n1-yes\n2-no\n");
gets(con);//get user input as string
fflush(stdin);
cont=atoi(con);//convert stringt to int
if(cont!=1&&cont!=2)
printf("Invalid Input!\n\n");
}while(cont!=1&&cont!=2);

return cont;
}


int winnercheck(char player)//check if player has won he game
{
int i,j,win=0;
int check[3][3];

//copy content of array to a new array for testing
if(player=='x')
for(i=0;i<3;i++)
for(j=0;j<3;j++)
check[i][j]=checkX[i][j];
else
for(i=0;i<3;i++)
for(j=0;j<3;j++)
check[i][j]=checkO[i][j];

for(i=0;i<3;i++)
{
//check vertical
if(check[0][i]==1&&check[1][i]==1&&check[2][i]==1)
win=1;
}

for(i=0;i<3;i++)
{
//check horizontal
if(check[i][0]==1&&check[i][1]==1&&check[i][2]==1)
win=1;
}

//check diagonal "\"
if(check[0][0]==1&&check[1][1]==1&&check[2][2]==1)
win=1;

//check diagonal "/"
if(check[0][2]==1&&check[1][1]==1&&check[2][0]==1)
win=1;

return win;
}

void players(char player)//call player X or O turns
{
char inrow[100],incol[100];
int i,j,rrow,rcol,error,win;
char row[10],col[10];

do{
error=0;
system("cls");

print_table();//call func print 3x3 table
printf("%s, please make your move\n",(player=='x'?nameX:nameO));
printf("Enter row number: ");
fflush(stdin);
gets(inrow);//get row input
fflush(stdin);
printf("Enter column number: ");
gets(incol);//get column input
fflush(stdin);

if(strlen(incol)==0 || strlen(inrow)==0)//check if input is empty
{
printf("Invalid Input!\n");
system("pause");
error=1;
}
else
{

//strip white space from input
for(i=0,j=0;inrow[i]!='\0';i++)
{
if (inrow[i] != ' ' && inrow[i] != '\t')
row[j++]=inrow[i];
}
row[j]='\0';

for(i=0,j=0;incol[i]!='\0';i++)
{
if (incol[i] != ' ' && incol[i] != '\t')
col[j++]=incol[i];
}
col[j]='\0';

//check for illegal char in string
for(i=0;i {
if(inrow[i]>57 || inrow[i]<48 )
error=1;
}

for(i=0;i {
if(incol[i]>57 || incol[i]<48)
error=1;
}

if(error!=1)
{//if no error convert string to int
rrow=atoi(inrow);
rcol=atoi(incol);

if(rrow<0 || rrow>2 || rcol<0 || rcol>2)
{//check if value entered is in range
printf("Invalid input!\n");
system("pause");
error=1;
}
else if(checkO[rrow][rcol]==1 || checkX[rrow][rcol]==1)
{//check if the selected box is occupied
printf("\nThat box is already occupied!\n");
system("pause");
error=1;
}
else//mark selected box with X or O
(player=='x'?checkX[rrow][rcol]=1:checkO[rrow][rcol]=1);
}
else
{
printf("Invalid Input!\n");
system("pause");
}
}


}while(error==1);


win=winnercheck(player);//check if player won
if(win==1)
won(player);
else if(check_draw()==1)//check if the game is a draw
{
system("cls");
printf("\n\n\t\tIt is a Draw!\n");
}
else//if none win or draw, continue game with opposite player
players((player=='x'?'o':'x'));

return;
}

void won(char player)
{//clear screen then print winning player
system("cls");
if(player=='x')
printf("\n\n\t\t%s won the game.!!\n",nameX);
else
printf("\n\n\t\t%s won the game.!!\n",nameO);

return;
}

int check_draw()
{//function to check if any player has won the game
int sum=0,i,j;

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum+=checkX[i][j];
sum+=checkO[i][j];
}
}

if(sum==9)//if all boxes are filled(draw)
return 1;//return 1 if draw
else
return 0;//return 0 if not draw
}


/**********************************************************************

* This program uses structures to store groups of data. *
* The program will run maximum of 40 times and will terminate before *
* that if the operator decides to stop the program. *
* All user input were taken as string to check for error and later *
* convert to a proper format for calculationa or storage. *
* String for input were given plenty of memory space to prevent *
* gets() function from giving error when operator input longer *
* than assigned memory space. *
**********************************************************************/

//preprocessors directives
#include
#include
#include
#include

//constant definitions
#define L3 1.2
#define T6 1.0
#define M6 0.8

//structures
struct private_info{
char name[100];
int age;
char gender[7];
};

struct shop{
char shopname[100];
char shopadd[100];
char tel[12];
}shopDetail={"CLEAN LAUNDRY","NO. 12,JALAN PAHANG,KUALA LUMPUR.","03-12345678"};

typedef struct customer_detail{
struct private_info info;//structure in structure
float weight;
float payment;
}CUSTOMER;//define structure as CUSTOMER

//functions definitions
CUSTOMER get_cust();
void print_receipt(CUSTOMER);
void summary(int i,CUSTOMER cus[40]);
float calc(float weight);
int cont();

//main function
int main()
{
CUSTOMER cus[40];
int con=1,i;
//program run max 40 times and terminate or when con==0
for(i=0;i<40&&con==1;i++)
{
system("cls");//clear screen
cus[i]=get_cust();//store data into structure array
cus[i].payment=calc(cus[i].weight);//calc and store payment
print_receipt(cus[i]);//print receipt
if(i!=39)//if 40th iteration, do not prompt to continue
con=cont();//prompt to continue
}
summary(i,cus);//print summary
system("pause");//pause program when done
return 0;
}

CUSTOMER get_cust()
{
//extra long string to avoid corruption due to gets() *should use fgets()*
char temp_age[100],temp_gender[100],temp_weight[100];
int error,i,count;
CUSTOMER cus;

do{//repeat if invalid input
printf("Please enter customer's name: ");
gets(cus.info.name);//get name
fflush(stdin);//flush input buffer

if(strlen(cus.info.name)==0)//check if name is empty
printf("Invalid Input!\n");
else
{ //make every 1st char of word to be uppercase
cus.info.name[0]=toupper(cus.info.name[0]);
for(i=0;i if(isspace(cus.info.name[i]))
cus.info.name[i+1]=toupper(cus.info.name[i+1]);
}
}while(strlen(cus.info.name)==0);

do{//repeat if invalid input
error=0;
printf("Age: ");
gets(temp_age);//get age as string
fflush(stdin);//flush input buffer

if(strlen(temp_age)==0)//check if input is empty
{
error=1;
printf("Invalid Input!\n");
}
else
{ //check for unwanted char in string
for(i=0;i {
if(!isdigit(temp_age[i]))
error=1;
}
}

if(atoi(temp_age)==0)//age zero not allowed to enter the shop *laugh*
error=1;

if(error==1)
{
printf("Invalid Input!\n");
}
else //convert string to int and store into structure
cus.info.age=atoi(temp_age);
}while(error==1);

do{//repeat if invalid
error=0;
printf("Gender (M/F): ");
gets(temp_gender);//get gender as string
fflush(stdin);

if(strlen(temp_gender)>1)//make input invalid if length >1
{
error=1;
printf("Invalid Input!\n");
}
else if(temp_gender[0]!='M' && temp_gender[0]!='m' && temp_gender[0]!='F' && temp_gender[0]!='f')
{//allow 'm','M','f','F' only
error=1;
printf("Invalid Input!\n");
}
else
{//if no error,
cus.info.gender[0]=tolower(temp_gender[0]);
}

}while(error==1);

printf("---------------------------------------------\n");
printf("| WEIGHT | Price per kilogram |\n");
printf("---------------------------------------------\n");
printf("| Less than 3 kg | RM %.2f |\n",L3);
printf("---------------------------------------------\n");
printf("| 3kg to 6kg | RM %.2f |\n",T6);
printf("---------------------------------------------\n");
printf("| More than 6kg | RM %.2f |\n",M6);
printf("---------------------------------------------\n");

do{//repeat if invalid input
error=0;
printf("Enter the weight of laundry\n");
gets(temp_weight);//get weight as string
fflush(stdin);

if(strlen(temp_weight)==0)
{
printf("Invalid Input!\n");
error=1;
}
else
{
count=0;
for(i=0;i {//check for unwanted char in string, allow digit and dot only
if(temp_weight[i]==46)//allow only 1 dot
count++;
if( (!isdigit(temp_weight[i]) && temp_weight[i]!=46) || count>1)
error=1;
}
if(error==1)
printf("Invalid Input!\n");
else
cus.weight=atof(temp_weight);//convert string to num
}

}while(error==1);

return cus;
}


void print_receipt(CUSTOMER cus)
{
float rate;

if(cus.weight<3)
rate=L3;
else if(cus.weight>=3 && cus.weight<=6)
rate=T6;
else if(cus.weight>6)
rate=M6;

//clear screen and print receipt
system("cls");
printf("\t OFFICIAL RECEIPT\n\n");
printf("\t\t%s\n",shopDetail.shopname);
printf("\t%s\n",shopDetail.shopadd);
printf("\t TEL:%s\n",shopDetail.tel);
printf("-----------------------------------------------\n\n");
printf(" Customer Name\t\t: %s\n",cus.info.name);
printf(" Age\t\t\t: %d\n",cus.info.age);
printf(" Gender\t\t: %s\n",(cus.info.gender[0]=='m'?"Male":"Female"));
printf(" Weight of laundry\t: %.2fkg\n",cus.weight);
printf(" Price per kilogram\t: RM%.2f\n",rate);
printf(" ------------------------------------\n");
printf(" Subtotal \t\t: RM%.2f\n",cus.payment);
printf(" ------------------------------------\n");
printf("\n\n\n\n\n\n\n\n");

return;
}

int cont()
{
char con[12],con1[]="CONTINUE",con2[]="STOP";
int cont,i;

do{//prompt to continue
printf("Do you want to continue (\"CONTINUE\"/\"STOP\") ?\n");
gets(con);
fflush(stdin);

if(strlen(con)==0)//check if input is empty
printf("Invalid Input!\n");
else
{ //convert input to uppercase
for(i=0;i con[i]=toupper(con[i]);
//compare processed input with predefined string
if(strcmp(con,con1)==0)
cont=1;
else if(strcmp(con,con2)==0)
cont=0;
else
printf("Invalid Input!\n");
}
}while(cont!=1 && cont!=0);

return cont;
}

void summary(int i,CUSTOMER cus[40])
{
int j;
float sum=0;

//calc total payment
for(j=0;j sum+=cus[j].payment;

//clear screen and print summary
system("cls");
printf("\tTotal customer today : %d\n",i);
printf("\tTotal payment today : RM%.2f\n\n\n",sum);

return;
}

float calc(float weight)
{
float pay;
//calc amount needed to pay
if(weight<3)
pay=weight*L3;
else if(weight>=3 && weight<=6)
pay=weight*T6;
else if(weight>6)
pay=weight*M6;

return pay;//return amount
}

No comments:

Post a Comment