Tic-Tac-Math is a lot like Tic-Tac-Toe. The game is played on a 3x3 grid; player
ID: 3805323 • Letter: T
Question
Tic-Tac-Math is a lot like Tic-Tac-Toe. The game is played on a 3x3 grid; players take turns marking one cell at a time. The difference is that instead of placing Xs and Os on the grid, the players place the numbers 1 to 9. And, instead of trying to make a row, column, or diagonal filled with 3 Xs or 3 Os, the players try to make a row, column, or diagonal whose cells sum to 15. One wrinkle: each number can only be placed ONCE.
Let’s think about implementing this game. (For now, DON'T USE CLASSES; everyone has different amounts of experience with them, so for now we’ll just use functions.) We have a couple questions to think about before we start coding:
1) How should we represent the board? You might be tempted to say “a two-dimensional array!” But C++’s multi-dimensional arrays cause great confusion even among experienced programmers, so I stay away from them. In this case, a simple way to start is to represent the board as a 9-element array of int.
2) And what behaviors (functions) do we need? ere’s infinite variety here, but generally speaking we try to have each function to one small well-defined task, So (in addition to main(), of course), we might have
A function to getAMove() from the user (we’ll assume that two players are sharing one keyboard).
A function to validate() the input. (Make sure the input values are in legal ranges.)
A function to playAMove() onto the board (this function will have to do a li le more
validation)
A function to display() the current state of the board.
A function to checkForWin().
Notice I haven’t given you the complete function signatures, and I certainly haven’t described how they should be used (are they all called from main()? Or do some of these call the others?)
PLEASE DO NOT USE CLASSES NOR 2D ARRAYS
Please make files called board.h board.cc input.h input.cc main.cc makefile
board.cc will have functions playAMove, display, checkForWin
input.cc will have functions getAMove, validate
Thank you very much!
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <math.h>
/* Complete the function below to print 2 integers separated by a single space which will be your next move
*/
void nextMove(char player, char ar[3][3]){
}
int main(void) {
int i;
char player;
char board[3][3];
//If player is X, I'm the first player.
//If player is O, I'm the second player.
scanf("%c", &player);
//Read the board now. The board is a 3x3 array filled with X, O or _.
for(i=0; i<3; i++) {
scanf("%s[^ ]%*c", board[i]);
}
nextMove(player,board);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.