Question
Hi, I REALLY NEED the rest of the code for this function. The language is C. I already have part of the Code. Please can
Hi, I REALLY NEED the rest of the code for this function. The language is C. I already have part of the Code. Please can you finish writing it for me? I will send the most part of the code that I already have. Thank you!
This is the code I have so far (with comments, function declarations and their definitions). PLEASE HELP ASAP. THANK YOU!:
/* File: tictactoe.c * This program allows two players to play tic-tac-toe. It makes sure that * a move is valid and determines if there is a winner.
* * Assignment: In-class Exercise 8 * Programmer: Rafael P. */
#include
/* Constants */ #define TRUE 1 /* boolean true/false */ #define FALSE 0 #define BOARD_SIZE 3 /* tic-tac-toe board is 3x3 */ #define MAX_POSITIONS 9 /* maximum valid position */ #define MAX_TURNS MAX_POSITIONS /* another name for readbility */
/* Function prototypes */ void instructions(); int place_move (char board[BOARD_SIZE][BOARD_SIZE], char player, int position);
/* ********* WRITE IN CLASS ********* */ int initialize_board (char board[BOARD_SIZE][BOARD_SIZE]); // sets the starting chars) void print_board(); /*receives and prints the current game board*/ /* check_for_winner - receives the game board and returns the winner as a char */
int main () { char tictactoe_board[BOARD_SIZE][BOARD_SIZE], /* game board */ player, /* player's turn, X or O */ winner; /* winning player */ int position, /* position chosen by player */ turns; /* # of turns taken so far */
/* Greet the user and give instructions */ instructions();
/* Initialize game */ player = 'X'; /* X goes first */ winner = '-'; /* no winner, yet */ turns = 0;
/* Initialize game board */
/* Display game board */
/* Play while there is no winner and there are still turns left */ while ((winner == '-') && (turns
/* Place the move and check if valid */ if (place_move (tictactoe_board, player, position)) { /* Move is valid. Check for a winner ****** ****** TO BE WRITTEN IN CLASS ****** ****** For now, just set winner to '-' so we can test the rest of the program. TO BE COMPLETED AS PART OF IN-CLASS EXERCISE. */ winner = '-';
/* Increment number of turns taken */ turns++;
/* Make it the next player's turn */ if (player == 'X') player = 'O'; else player = 'X'; } /* end if valid move */ else printf ("That was an invalid move. Please try again. ");
/* Display game board - ****** ****** TO BE WRITTEN IN CLASS ****** ****** */
} /* end while */
/* Print out the winner */ if (winner != '-') printf (" Player %c has won. ", winner); else printf (" The game has ended in a draw. ");
return 0; } /* end main */
/* Function: instructions * Prints the instructions for using this program */ void instructions () { printf (" Welcome to the tic-tac-toe game server. Please decide who "); printf ("will be Player X and who will be Player O. Player X always goes "); printf ("first. The positions on the board are numbered. Please enter a "); printf ("position number between 1 and 9 when asked. This program will "); printf ("check for a winner after every turn. Have fun! "); } /* end instructions */
/* Function: place_move * Objects Type Movement Name * ---------------------------------------------------------------------- * Tic-tac-toe board char [][] rec'd, pb board * Player whose turn it is char received player * Chosen position int received position * Whether the move is valid int returned valid */ int place_move (char board[BOARD_SIZE][BOARD_SIZE], /* REC'D & P'BACK: game board */ char player, /* REC'D: whose turn it is */ int position) /* REC'D: chosen position */ { int row, column, /* indexes */ valid; /* returned object - status of placement */
/* Assume position is okay */ valid = TRUE;
/* Check for valid position number */ if ((position MAX_POSITIONS)) /* out of range */ valid = FALSE; else { /* Compute row and column indexes */ row = (position - 1) / 3; column = (position - 1) % 3;
/* Check that position is not taken */ if ((board[row][column] != 'X') && (board[row][column] != 'O')) /* Set the board square to the player */ board[row][column] = player; else valid = FALSE; } /* end else valid position */
/* Return the result */ return valid; } /* end place_move */
In-class Exercise 8 (10 points) The purpose of this exercise is to work with multi-dimensional arrays. An empty CodeBlocks project should be created for this exercise, and the program file tictactoe.c should be downloaded from Blackboard. Problem Statement Write a tic-tac-toe game server that allows two users to play and keeps track of the moves made by each. It should not allow invalid moves, and it should display the board a wins. fter every turn. It should determine when som eone Behavior In the game of tic-tac-toe, two players, named X and O, take turns marking positions on a 3 by 3 grid. A player wins a game of tic-tac-toe if she is able to mark three adjacent positions in any orientation (i.e., a whole row, a whole column, or either diagonal). If all positions are marked without a producing a winner, the game is a draw The program should initialize the board so that the board positions are numbered (as characters) like so 1 2 3 4 5 6 7 8 9 It then should ask Player X and Player O to take turns choosing positions (as numbers). Player X always goes first. The program should not allow players to choose positions that are out of range or that have already been marked. When a position is chosen, the position number should be replaced by the player's character, and the changed board displayed. For example, the board in the middle of a game might look like 4 5 6 O 8 9 The program winner or if th should check for a winner after each turn and print out the an appropriate message when there is a e game ends in draw Page I of 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started