the tic-tac toe program

creating a tic tac toe program using just stdio.h and functions

Page 1 of 1

2 Replies - 33190 Views - Last Post: 29 October 2009 - 08:26 AM Rate Topic: -----

#1 apartbyhorses  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 28-October 09

the tic-tac toe program

Posted 28 October 2009 - 06:34 PM

Okay, so I created this tic tac to program using a text book i own:

#include <stdio.h>

int main (void)
{
	int player = 0;
	int winner = 0;
	int choice = 0;
	int row = 0;
	int column = 0;
	int line = 0;

	char board [3][3] = {
				 {'1','2','3'},
				 {'4','5','6'},
				 {'7','8','9'}
						};


	for (int i = 0; i<9 && winner==0; i++)
	{
		printf("\n\n");
		printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
		printf("---|---|---\n");
		printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
		printf("---|---|---\n");
		printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);

		player = i%2 + 1;

		do
		{
			printf("\nPlayer %d, please enter the number of the square "
				"where you want to place your %c: ",
				player,(player==1)?'X':'O');
			scanf("%d", &choice);


			row = --choice/3;
			column = choice%3;
		}while(choice<0 || choice>9 || board [row][column]>'9');

		board[row][column] = (player == 1) ? 'X' : 'O';


		if((board[0][0]==board[1][1] && board[0][0]==board[2][2]) ||
		   (board[0][2]==board[1][1] && board[0][2]==board[2][0]))
		   winner = player;
		else
			for(line = 0; line <=2; line++)
				if((board[line][0]==board[line][1] && board[line][0]==board[line][2])||
					(board[0][line]==board[1][line] && board[0][line]==board[2][line]))
					winner = player;
	

	}

		printf("\n\n");
		printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
		printf("---|---|---\n");
		printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
		printf("---|---|---\n");
		printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);


		if(winner==0)
			printf("The game is a draw\n");
		else
			printf("Player %d has won\n", winner);

	return 0;
}



However, i would like to know how to turn this into a program using functions. i need at least the functions for printing the board, checking for valid input and checking for a win. however, i can't seem to get this.

Is This A Good Question/Topic? 1

Replies To: the tic-tac toe program

#2 chromeman605  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-August 09

Re: the tic-tac toe program

Posted 29 October 2009 - 06:54 AM

Try this code out..

first the header file named "tic.h"

#include <stdio.h>
void print_grid(char board[4][4]);
int didwin(char board[4][4]);




the the program with functions




  //Purpose: Two players battle in the classic game tic tac toe until one is proclaimed victor!




#include "tic.h"

int main(void){
  int i=0,j=0,player=0,lead=0,nrows=0,ncols=0,winner=0;
  char again='q';
  char four_X_four[4][4];
  while(winner == '\0') {
	for(i = 0; i < 4; i++) {
	  for(j = 0; j<4; j++) {
	four_X_four[i][j] = '\0';
	  }
	}
	for( i = 0; i<16 && winner==0; i++)
	  { 
		print_grid(four_X_four);
	player = i%2 + 1;
	if(player==1){
	  printf("\n Player %d, please enter the number of the square "
		 "where you want to place your %c: ",player,'X');
	}
	else if(player==2) {
	  printf("\n Player %d, please enter the number of the square "
		 "where you want to place your %c: ", player,'O');
	}
	scanf("%d", &lead);
	lead--;
	ncols = lead%4;
	lead = lead - ncols;
	nrows = lead/4;  
	if(lead<0 || lead>16 || four_X_four[nrows][ncols]=='X' || four_X_four[nrows][ncols]=='O') {
	  printf("Space is already taken, please try again");	
	  i--;
	}
	else {
	  four_X_four[nrows][ncols] = (player == 1) ? 'X' : 'O';
	}
	winner = didwin(four_X_four);
	  }
	if(winner != '\0') {
	  printf("Winner was %c! Good job.\n",winner);
	  printf("Do you want to play again? y/n: ");
	  while(!(again == 'y' || again == 'n')) {
		scanf("%c",&again);
	  }
	  if(again == 'y') {
		winner = '\0';
		again = 'q';
	  }
	}
	else {
	  printf("No winner this round. Try again.");
	}
  }
  return 0;
}
void print_grid(char board[4][4]) {
  int i,j;
  printf("\n\n");
  for(i = 0; i < 4; i++) {
	for(j = 0; j < 4; j++) {
	  if(board[i][j] == '\0') {
	printf(" %2d ", 4*(i)+(j+1));
	  }
	  else {
	printf(" %c  ", board[i][j]);
	  }
	  if(j!=3) { printf("|"); }
	}
	if(i != 3) {
	  printf("\n-------------------\n");
	}
  }
}

int didwin(char board[4][4]) {
  int i,j;
  char current;
  char winner = '\0';

  //Iter over rows to check for winner
  for(i = 0; i<4; i++) {
	current = board[i][0];
	for(j = 0; j < 4; j++) {
	  if(board[i][j] != current) {
	current = '\0';
	  }
	}
	if(current != '\0') {
	  winner = current;
	}
  }

  //Iter over columns
  for(i = 0; i<4; i++) {
	current = board[0][i];
	for(j = 0; j < 4; j++) {
	  if(board[j][i] != current) {
	current = '\0';
	  }
	}
	if(current != '\0') {
	  winner = current;
	}
  }

  //Iter over diagonals
  current = board[0][0];
  for(i = 0; i < 4; i++) {
	if(board[i][i] != current) {
	  current = '\0';
	}
  }
  if(current != '\0') {
	winner = current;
  }
  
  current = board[0][4];
  for(i = 0; i <4; i++) {
	if(board[i][4-i] != current) {
	  current = '\0';
	}
  }
  if(current != '\0') {
	winner = current;
  }
  return winner;
}




this should work!! Good luck!

This post has been edited by chromeman605: 29 October 2009 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: the tic-tac toe program

Posted 29 October 2009 - 08:26 AM

I'll do a couple of them. You can figure out the rest.

#include <stdio.h>

#define ROWS 3
#define COLS 3
typedef char Board[ROWS][COLS];

void printBoard(Board board) {
	printf("\n\n");
	printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
	printf("---|---|---\n");
	printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
	printf("---|---|---\n");
	printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
}

int hasWinner(Board board) {
	if((board[0][0]==board[1][1] && board[0][0]==board[2][2]) ||
		(board[0][2]==board[1][1] && board[0][2]==board[2][0])) { return 1; }
	for(line = 0; line <=2; line++) {
		if((board[line][0]==board[line][1] && board[line][0]==board[line][2])||
			(board[0][line]==board[1][line] && board[0][line]==board[2][line])) {
				return 1;
			}
	}
	return 0;
}

int main() {
	int winner = 0;
	int choice = 0;
	int row = 0;
	int column = 0;
	int line = 0;
	int i;

	Board board = {
		{'1','2','3'},
		{'4','5','6'},
		{'7','8','9'}
	};


	for (i = 0; i<9 && !winner; i++) {
		int player = i%2 + 1;
		printBoard(board);

		do {
			printf("\nPlayer %d, please enter the number of the square "
				"where you want to place your %c: ",
				player,(player==1)?'X':'O');
			scanf("%d", &choice);

			row = --choice/3;
			column = choice%3;
		} while(choice<0 || choice>9 || board [row][column]>'9');

		board[row][column] = (player == 1) ? 'X' : 'O';
		if hasWinner(board) { winner = player; }
	}
	
	printBoard(board);

	if(!winner) {
		printf("The game is a draw\n");
	} else {
		printf("Player %d has won\n", winner);
	}

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1