Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- * Description : Tic Tac Toe game
- * Author : dsl
- *
- * Write a program that lets two humans play a game of Tic Tac Toe in a
- * terminal. The program should let the players take turns to input their
- * moves. The program should report the outcome of the game.
- *
- * During your interview, you will pair on adding support for a computer player
- * to your game. You can start with random moves and make the AI smarter if you
- * have time.
- ****************************************************************************/
- #include <stdio.h>
- int isWinner();
- int status;
- int move;
- int selected;
- int player = 1;
- char position[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
- char playerSymbol;
- void gameBoard()
- {
- int boardSize = sizeof(position)/sizeof(position[0]);
- for (int i = 0; i < boardSize; i++) {
- printf(" %c |", position[i]);
- if ( i == 2 || i == 5) {
- printf("\n------------\n");
- }
- }
- }
- int play()
- {
- const int MIN = 0;
- const int MAX = 9;
- status = isWinner(); // -1 when in play
- while (status == -1) {
- if (player == 1) {
- playerSymbol = 'X';
- } else {
- playerSymbol = 'O';
- }
- printf("\n\nOk %c, it's your move. Which square? ", playerSymbol);
- do {
- scanf("%d", &move);
- if (move < MIN || move > MAX) {
- printf("\nWrong input\n");
- fflush(stdin);
- }
- } while (move < MIN || move > MAX);
- printf("\n\n");
- selected = move - 1 ;
- if (player == 1) {
- position[selected] = 'X';
- player--;
- } else {
- position[selected] = 'O';
- player++;
- }
- gameBoard();
- status = isWinner();
- }
- if (status == 1) {
- printf("\nWe have a WIENER.\n");
- if (player == 0) {
- printf("X wins!\n");
- } else if (player == 1) {
- printf("O wins!\n");
- }
- }
- return 0;
- }
- int isWinner()
- {
- if (position[0] == position[1] && position[1] == position[2]) {
- return 1;
- } else if (position[3] == position[4] && position[4] == position[5]) {
- return 1;
- } else if (position[6] == position[7] && position[7] == position[8]) {
- return 1;
- } else if (position[0] == position[3] && position[3] == position[6]) {
- return 1;
- } else if (position[1] == position[4] && position[4] == position[7]) {
- return 1;
- } else if (position[2] == position[5] && position[5] == position[8]) {
- return 1;
- } else if (position[0] == position[4] && position[4] == position[8]) {
- return 1;
- } else if (position[2] == position[4] && position[4] == position[6]) {
- return 1;
- } else if (position[0] != '1' && position[1] != '2' && position[2] != '3' && position[3] != '4' && position[4] != '5' && position[5] != '6' && position[6] != '7' && position[7] != '8' && position[8] != '9'){
- printf("\nYou both failed to best each other. For shame! Game is null.\n");
- return 0;
- } else {
- return -1;
- }
- }
- int main()
- {
- printf("Hello, players!\nX starts.\n\nHere be the board: \n\n");
- gameBoard();
- play();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement