Mostafa
Mostafa

Reputation: 69

How to get a variable of type int to have a value of a character in C

What I specifically want to do is have a for loop iterating through the board array and when it reaches the one that the user has inputted, it replaces its value (the number on the board) with an X, so that the user has placed an X on the Tic Tac Toe board. I have set up the for loop the way I thought that it should be done, but I have left out the result of the loop and replaced it with comments.

#include <stdio.h>
int main()
{
    int tLeft = 0;
    int tMid = 1;
    int tRight = 2;
    int mLeft = 3;
    int mMid = 4;
    int mRight = 5;
    int bLeft = 6;
    int bMid = 7;
    int bRight = 8;
    int userChoice, compChoice, i;
    int board[10] = {
        tLeft, tMid, tRight,
        mLeft, mMid, mRight,
        bLeft, bMid, bRight
    };

    printf("[%d][%d][%d]\n", tLeft, tMid, tRight);
    printf("[%d][%d][%d]\n", mLeft, mMid, mRight);
    printf("[%d][%d][%d]\n", bLeft, bMid, bRight);
    printf("Enter the number you would like to place an X at: ");
    scanf("%d", userChoice);

    for(i = 0; i < 9; i++)
    {
        if(board[i] == userChoice)
        {
            // MAKE BOARD[i] EQUAL TO X. THE PROBLEM WITH THIS IS THAT
            // BOARD[i] IS TYPE INT AND 'X' WOULD BE TYPE CHAR AND I
            // MIGHT SOUND LIKE A HUGE NOOB BUT I JUST STARTED CODING
            // IN C HALF AN HOUR AGO.
        }
    }

    return 0;
}

Upvotes: 1

Views: 181

Answers (3)

chux
chux

Reputation: 153447

OP has initialized board[] with 0, 1, 2, 3, ... which serves little purpose.

Better to write to the board[] with one of 3 values: X, O or empty.

As OP is starting out, use: 'X', 'O', ' '

char board[10] = { ' ', ' ', ' ',  ' ', ' ', ' ',  ' ', ' ', ' '  };  

A simple print board state could use

printf("[%c][%c][%c]\n", board[tLeft], board[tMid], board[tRight]);
.. 2 more lines

After reading input, test if used

for (;;) {
  if (scanf("%d", userChoice) != 1) { 
    puts("Non-numeric input, quitting");
    return -1;
  }
  if (userChoice < 1 || userChoice > 9) { 
    puts("Out of range input, Try again");
    continue;
  }
  if (board[userChoice - 1] != ' ')) { 
    puts("Square all ready used, Try again");
    continue;
  }
}
board[userChoice - 1] != 'X';

OP is about 20% done with writing a 2 player TTT game. Good luck with the rest. Many simplifications possible.

Upvotes: 0

ruakh
ruakh

Reputation: 183290

You can write:

board[i] = 'X';

It's true that 'X' is a character constant, but for odd historical reasons, character constants in C actually have type int. (See Why are C character literals ints instead of chars?.)

Furthermore, even if character constants did have type char, there'd still be no problem. The C compiler can implicitly "promote" a char to an int for you:

char x = ...;
board[i] = x;   // allowed!

That being said, you might want to re-examine your decision to have board be an int[] rather than a char[] . . . after all, when you print out your board, you'll need to have some way to print the X's. So you're better off using '1' than 1.

Upvotes: 4

Ivan Gritsenko
Ivan Gritsenko

Reputation: 4236

You can operate with char array

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

then you do

scanf("%d", userChoice);
board[userChoice] = 'X';

So you board actually store you game state. It can be printed any time like you did but with %c specifier.

Note: if you want to convert digit character to int use this trick int digit = c - '0';

Upvotes: 0

Related Questions