All pastes #428228 Raw Edit

Stuff

public text v1 · immutable
·published 2007-04-07 11:46 UTC
rendered paste body
#include <stdio.h>
#include <time.h>
#include <ncurses.h>
// adding 9x9, {}
char nine[5][5]={'$', '$', '$', '$',
		 '$', '$', '$', '$',
		 '$', '$', '$', '$',
		 '$', '$', '$', '$'};
int xturn();
int oturn();
int checkwinner();
int showboard();
int checkwhatodo();
int restart();


int main()
{
	int b, board, i;
	char x, o;
	
	printf("Hello, this is an tic tac toe game made by jimbo.");
	printf("\n\nPlease enter the number 1 for 3x3 board or 2 to exit: ");
	scanf("%d", &board);
	if(board==1) {
	printf("Welcome the size of the board is 3x3, \nthe the number of players is 2.\n\n");
	printf("Player1 - x, Player2 - o.\n\n");	
	printf("%c|%c|%c\n", nine[1][1], nine[1][2], nine[1][3]); 
	printf("------\n"); 
	printf("%c|%c|%c\n", nine[2][1], nine[2][2], nine[2][3]); 
	printf("------\n"); 
	printf("%c|%c|%c\n", nine[3][1], nine[3][2], nine[3][3]);
	printf("\nThis is the board.\nProgram rolls who begins.");
	printf("Please enter who begins(x=1, o=2): ");
	if(i==1) {
	printf("Player1 begins.");
	xturn();
	oturn();
	xturn();
	oturn();
	xturn();
	oturn();
	xturn();
	oturn();
	xturn();
	} else if(i==2) {
	printf("Player2 begins.");
	oturn();
	xturn();
	oturn();
	xturn();
	oturn();
	xturn();
	oturn();
	xturn();
	oturn();
	}
	
	// for(b=1; b<9; b++);
	} else if(board==2) {
	return 0;	
	} else {
	printf("Wrong Input, quitting");
	return 0; 
	}
	return 0;
}
int showboard()
{
	printf("\n\n");
	printf("%c|%c|%c\n", nine[1][1], nine[1][2], nine[1][3]); 
	printf("------\n"); 
	printf("%c|%c|%c\n", nine[2][1], nine[2][2], nine[2][3]); 
	printf("------\n"); 
	printf("%c|%c|%c\n", nine[3][1], nine[3][2], nine[3][3]);
}
int checkwhatodo()
{
	int a;
	printf("\n\nPlease enter 1 to restart or 2 to quit.");
	scanf("%d", &a);
	if(a==1) {
	restart();
	} else if(a==2) {
	return 0;
	} else { 
	printf("\n\nInput is wrong, exitting anyway.");
	}
}
int restart() // ADD CLEAR SCREEN COMMAND
{
	int board, x, y, i;
	ncurses_clear (main);
	printf("\n\nPlease enter the number 1 for 3x3 board or 2 to quit: ");
	scanf("%d", &board);
	if(board==1) {
	printf("Welcome the size of the board is 3x3, \nthe the number of players is 2.\n\n");
	printf("Player1 - x, Player2 - o.\n\n");	
	printf("%c|%c|%c\n", nine[1][1], nine[1][2], nine[1][3]); 
	printf("------\n"); 
	printf("%c|%c|%c\n", nine[2][1], nine[2][2], nine[2][3]); 
	printf("------\n"); 
	printf("%c|%c|%c\n", nine[3][1], nine[3][2], nine[3][3]);
	printf("\nThis is the board.\nProgram rolls who begins.");
	for(y=0;y<5;y++) { 
	for(x=0;x<5;x++) { 
	board[x][y]='$'; } 
	}
	printf("Please enter who begins(x=1, o=2): ");
	scanf("%d", &i);
	if(i==1) {
	printf("Player1 begins.");
	xturn();
	oturn();
	xturn();
	oturn();
	xturn();
	oturn();
	xturn();
	oturn();
	xturn();
	} else if(i==2) {
	printf("Player2 begins.");
	oturn();
	xturn();
	oturn();
	xturn();
	oturn();
	xturn();
	oturn();
	xturn();
	oturn();
	}
	else if(board==2) {
	return 0;
	} else {
	printf("Input is wrong");
	}
	}	
}
int checkwinner() // checks who's the winner
{
	if(nine[1][1]=='o' && nine[2][2]=='o' && nine[3][3]=='o') { // checks \ :} 
	printf("O wins");
	checkwhatodo();
	} else if(nine[1][1]=='o' && nine[1][2]=='o' && nine[1][3]=='o') { // checks - :} upper row
	printf("O wins");
	checkwhatodo();
	} else if(nine[1][1]=='o' && nine[2][1]=='o' && nine[3][1]=='o') { // checks | :} first column
	printf("O wins");
	checkwhatodo();
	} else if(nine[3][1]=='o' && nine[3][2]=='o' && nine[3][3]=='o') { // checks - :} third row 
	printf("O wins");
	checkwhatodo();
	} else if(nine[3][1]=='o' && nine[2][2]=='o' && nine[1][3]=='o') { // checks / :}
	printf("O wins");
	checkwhatodo();
	} else if(nine[2][1]=='o' && nine[2][2]=='o' && nine[2][3]=='o') { // checks - :} middle row
	printf("O wins");
	checkwhatodo();
	} else if(nine[1][2]=='o' && nine[2][2]=='o' && nine[3][2]=='o') { // checks | :} middle column
	printf("O wins");
	checkwhatodo();
	} else if(nine[1][3]=='o' && nine[2][3]=='o' && nine[3][3]=='o') { // checks | :} third column
	printf("O wins");
	checkwhatodo();
	}
	if(nine[1][1]=='x' && nine[2][2]=='x' && nine[3][3]=='x') { // checks \ :} 
	printf("X wins");
	checkwhatodo();
	} else if(nine[1][1]=='x' && nine[1][2]=='x' && nine[1][3]=='x') { // checks - :} upper row
	printf("X wins");
	checkwhatodo();
	} else if(nine[1][1]=='x' && nine[2][1]=='x' && nine[3][1]=='x') { // checks | :} first column
	printf("X wins");
	checkwhatodo();
	} else if(nine[3][1]=='x' && nine[3][2]=='x' && nine[3][3]=='x') { // checks - :} third row 
	printf("X wins");
	checkwhatodo();
	} else if(nine[3][1]=='x' && nine[2][2]=='x' && nine[1][3]=='x') { // checks / :}
	printf("X wins");
	checkwhatodo();
	} else if(nine[2][1]=='x' && nine[2][2]=='x' && nine[2][3]=='x') { // checks - :} middle row
	printf("X wins");
	checkwhatodo();
	} else if(nine[1][2]=='x' && nine[2][2]=='x' && nine[3][2]=='x') { // checks | :} middle column
	printf("X wins");
	checkwhatodo();
	} else if(nine[1][3]=='x' && nine[2][3]=='x' && nine[3][3]=='x') { // checks | :} third column
	printf("X wins");
	checkwhatodo();
	}
}
int xturn()
{
	int r, c;
	printf("\nWhere to put X? ");
	scanf("%d%d", &r, &c);
	if(r==1 && c==1){ // add {} and values
	nine[1][1] = 'x';
	} else if(r==1 && c==2) {
	nine[1][2] = 'x';	
	} else if(r==1 && c==3) {
	nine[1][3] = 'x';
	} else if(r==2 && c==1) {
	nine[2][1] = 'x';
	} else if(r==2 && c==2) {
	nine[2][2] = 'x';
	} else if(r==2 && c==3) {
	nine[2][3] = 'x';
	} else if(r==3 && c==1) {
	nine[3][1] = 'x';
	} else if(r==3 && c==2) {
	nine[3][2] = 'x';
	} else if(r==3 && c==3) {
	nine[3][3] = 'x';
	}
	showboard();
}
int oturn()
{
	int r, c;
	printf("\nWhere to put X? ");
	scanf("%d%d", &r, &c);
	if(r==1 && c==1){ // add {} and values
	nine[1][1] = 'o';
	} else if(r==1 && c==2) {
	nine[1][2] = 'o';	
	} else if(r==1 && c==3) {
	nine[1][3] = 'o';
	} else if(r==2 && c==1) {
	nine[2][1] = 'o';
	} else if(r==2 && c==2) {
	nine[2][2] = 'o';
	} else if(r==2 && c==3) {
	nine[2][3] = 'o';
	} else if(r==3 && c==1) {
	nine[3][1] = 'o';
	} else if(r==3 && c==2) {
	nine[3][2] = 'o';
	} else if(r==3 && c==3) {
	nine[3][3] = 'o';
	}
	showboard();
}