8 Replies - 1096 Views - Last Post: 19 August 2012 - 02:28 AM Rate Topic: -----

#1 ayizkatya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-August 12

Tic-Tac-Toe Game in C (with game specifications)

Posted 18 August 2012 - 11:26 PM

Hello, I have this program I'm writing. The idea is a tictactoe game where the matrix size is n x n (where n is a user input).

Game specifications:
1. Assume n>=3.
2. If n is even and # of x and o are not equal, then the tic-tac-toe is INVALID.
3. If n is odd and the absolute difference between # of x and o is not 1, then the tic-tac-toe is INVALID.
4. WINNER: If # of x OR o == n.
5. If both x and o win, there is no TIE, rather the game is INVALID.

Here's what I have so far:
#include <stdio.h>
#include <string.h>
#define VALID 1
#define INVALID 0

int is_valid(char *str, int n);
int get_winner(char *str, int n);
void print2d(char *str, int n);

int main()
{
    //gets size of matrix (n x n)
    int n;
    scanf("%d", &n);

    //gets matrix values in string form
    char str[n][n];
    int i;
    for (i=0; i<n; i++)
    {
        scanf("%s", &(str[i][0]));
    }
    printf("\n");

    int validity = is_valid(*str, n);
    if(is_valid == INVALID)
        printf("INVALID GAME.");

    //checks winner
    int winner = get_winner(*str, n);
    if(winner == 1)
        printf("X WINS!");
    if(winner == 0)
        printf("O WINS!");

    //if both x and o win, INVALID GAME
    if(winner == -1)
        printf("INVALID GAME.");

    return(0);
}

                /*LE FUNCTIONS*/

//CHECKS IF STRINGS ARE VALID
//If n is even and a and b are not equal,
//then the tictactoe is invalid.
//If n is odd and the absolute difference between a and b is not 1,
//then the tictactoe is invalid.
int is_valid(char *str, int n)
{
    int freq_o=0, freq_x=0;
    int i;

    for(i=0; i<n; i++)
    {
        if(str[i] == 'x')
            freq_x++;
        if(str[i] == 'o')
            freq_o++;
    }

    if(n%2 != 0 && freq_x - freq_o != 1 || freq_o - freq_x != 1)
        return INVALID;
    if(n%2 == 0 && freq_x == freq_o)
        return INVALID;
    else
        return VALID;
}

//GETS THE WINNER
//Winner: if # of x OR o = n
//Invalid Game: if both x AND o = n
int get_winner(char *str, int n)
{
    int i, winner, a, b;
    for(i=0; i<n; i++)
    {
        if(str[i] == 'x')
        {
            winner = 1;
            a++;
        }
        if(str[i] == 'o')
        {
            winner = 0;
            b++;
        }
    }
    return winner;
}



But I'm kinda stuck at how to determine the winner...

Is This A Good Question/Topic? 0
  • +

Replies To: Tic-Tac-Toe Game in C (with game specifications)

#2 TwoOfDiamonds  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 200
  • Joined: 27-July 12

Re: Tic-Tac-Toe Game in C (with game specifications)

Posted 18 August 2012 - 11:51 PM

In my Tic-Tac-Toe game I checked for every possible win situation , but yet again , my board was a classic 3x3 one , but just know , by writing these words, I figured I could have done it with a for loop and little math way smaller and cleaner :)
Knowing that your board is n*n cells you can take a for loop that will compare each cell in a row/column with the first cell in that row/column until it hits an empty cell , a cell that was marked by the other player or when the row/column ends . If the row ended prematurely then nobody won by that row/column this turn and you proceed with the next ones . However, if you get to the end of the row , the player with it's mark on any of the cells in that row/column won .

Take a look here if you want . It's a simple TicTacToe game from Michael Dawson's Beginning C++ through game programming 3rd edition .

Also here is another TicTacToe game but it has a little bit of graphics and it was done with C++ and SFML .

I should comment both of them someday , even thought it's not that hard to understand .
Was This Post Helpful? 0
  • +
  • -

#3 ayizkatya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-August 12

Re: Tic-Tac-Toe Game in C (with game specifications)

Posted 18 August 2012 - 11:58 PM

Hey, I haven't thought of comparing them that way, though I think it'll be a bit complicated for me because of the possibility of diagonals... But I'll try it, thanks!
Was This Post Helpful? 0
  • +
  • -

#4 TwoOfDiamonds  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 200
  • Joined: 27-July 12

Re: Tic-Tac-Toe Game in C (with game specifications)

Posted 19 August 2012 - 12:05 AM

Actually no , it's not . Since n should be and odd number you can get all the numbers from diagonal in a single for-loop.
For example on the main diagonal it would be :
for (int i = 0 ; i <= n ; i++)
{
    printf ("%d", board[i][i]);
}



And here is a snippet on minor (secondary) diagonal . It could be useful though the format is awful .
Was This Post Helpful? 0
  • +
  • -

#5 ayizkatya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-August 12

Re: Tic-Tac-Toe Game in C (with game specifications)

Posted 19 August 2012 - 12:07 AM

Yeah, I just thought of it now, thanks
Was This Post Helpful? 0
  • +
  • -

#6 TwoOfDiamonds  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 200
  • Joined: 27-July 12

Re: Tic-Tac-Toe Game in C (with game specifications)

Posted 19 August 2012 - 12:17 AM

Glad I could help ^_^
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1937
  • View blog
  • Posts: 5,763
  • Joined: 05-May 12

Re: Tic-Tac-Toe Game in C (with game specifications)

Posted 19 August 2012 - 12:35 AM

Just very slightly off topic:

Quote

4. WINNER: If # of x OR o == n.


Notice that nothing in the specifications requires that the X's or O's have to be in a line or adjacent to each other. He! He! He!

Although, it might be a safe assumption that is what the teacher wants, remember that the computer can't read your mind, nor your teacher's mind.

Welcome to the world of incomplete customer specifications!

Now back on topic:
Note that allocating an array like you are doing on line 17 is non-standard C. Not all compiler support this. You should really allocate memory for your matrix using malloc(). I know it complicates the matrix access, but that's the reality of programming in C.

Check your line 65. It is supposed to return invalid if n is even and number of x's and o's are not the same. Your current code doesn't seem to enforce that condition.

Check your line 63. Consider the case when n is odd, and there are 5 x's and 6 o's. Although there in only an absolute difference of one between them, I think you'll find that your if statement will still declare the matrix as invalid.

You should write your code to check for a winner by taking a parameter that corresponds to the player symbol to check for a win condition. This is so that you won't be duplicating the code for x's and o's.
Was This Post Helpful? 0
  • +
  • -

#8 ayizkatya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-August 12

Re: Tic-Tac-Toe Game in C (with game specifications)

Posted 19 August 2012 - 02:25 AM

Oh, I thought that part was a given since it's a tic-tac-toe game after all...but I'll be more careful next time :)

Uhm, I dunno how to use malloc() yet, we haven't encountered that in our lessons yet. I changed it char str[100][100] to limit the n, though. Is that okay?

I corrected lines 63 and 65.

I'm still working on the last part.

Thanks for helping!
Was This Post Helpful? 0
  • +
  • -

#9 TwoOfDiamonds  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 200
  • Joined: 27-July 12

Re: Tic-Tac-Toe Game in C (with game specifications)

Posted 19 August 2012 - 02:28 AM

View Postayizkatya, on 19 August 2012 - 12:25 PM, said:

I changed it char str[100][100] to limit the n, though. Is that okay?


Actually , I think you should make sure that the user's input doesn't exceed 100 .
You can ask for input until the player puts a number from 3 to 100 . Otherwise you'll go out of array bounds .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1