I've made a tictactoe game using ncurses functions such as mvaddch to put x and o on my board. I need to somehow find the locations of the x's and o's so that i can win my game, and i have no idea how to do this.

Printable View

- 01-26-2013xTonyLeoTictactoe x and o locations.
I've made a tictactoe game using ncurses functions such as mvaddch to put x and o on my board. I need to somehow find the locations of the x's and o's so that i can win my game, and i have no idea how to do this.

- 01-26-2013anduril462
I suggest you keep a copy of the board in a more convenient "format" for checking whether a square is occupied, whether somebody won, or it's a cat's game. A tic-tac-toe board is basically a 3x3 array where each cell can be X, O or empty. A 3x3 array of char is good, you can intialize it to ' ' (space) for a new, empty board. As a player moves, place the 'X' or 'O' in the appropriate space. So take a declaration like char board[3][3].

Now, you will need functions to translate screen coordinates to your 3x3 board (for input) and board to screen for output. How exactly you do that depends on how you draw your board and how you get user input. You must know the position on the screen of the user input to draw it and put the X's and O's, so find a way to correlate that to your board. I think curses uses the upper-left corner as the origin, (0, 0). That also means that board[0][0] could represent the upper-left of your tic-tac-toe board.

Hope that helps. Can't help you much more without knowing how you display the board and get input. - 01-26-2013xTonyLeo
i drew the board with mvaddch function for example mvaddch (i,0, '|'); but 3 different times.

So are you saying to use the 3x3 array all empty like "char board[3][3];"?

i also drew my x and o with the same mvaddch function sooo it pretty much just replaces that space

with an x or o. Should i put a counter so that everytime and x or o is placed add a counter to the array? - 01-26-2013anduril462
You don't need a counter, you simply initialize the board to all spaces, like so:

Code:`char board[3][3];`

for (r = 0; r < ; r++) { // row

for (c = 0; c < 3; c++) { // column

board[r][c] = ' ';

}

}

Code:`board[1][2] = 'X';`

To check for a win, e.g. across the top row, you check if board[0][0], board[0][1] and board[0][2] are the same char ('X' or 'O'). If they are, that person won. You do the same for all 3 rows, all 3 columns, and both diagonals. You check after every move.

Note, you haven't provided an actual description of your screen, or exactly how you get input, so the following is just an example of how to do it. You need a way to map the screen coordinates to your board coordinates. Note, below, I am using coordinates "backward" from normal, i.e. with the y (vertical/row) coordinate first. That matches the indexing in the board array, and the order of parameters to mvaddch. For example, if your screen looks like:

Code:`| |`

-+-+-

| |

-+-+-

| |

When the user places an X or an O on your board, you must know the position of that X or O, so you can draw it correctly (you did say you can draw the board fine with mvaddch). You pass mvaddch(y, x, ch) a y (row) and x (column) to draw the character (ch). You simply need to find a way to map the y and x screen coordinates to row/column for your board. From the above example

Code:`y (screen) -> row (board)`

10 -> 0

12 -> 1

14 -> 2

x (screen) -> column (board)

25 -> 0

27 -> 1

29 -> 2

Work with that for a while, then if you're still stuck post your code and me or somebody else will be along to help. - 01-26-2013xTonyLeo
So after i made the 2d array would i use if statements to change the array values as the person enters a number?

- 01-26-2013smokeyangel
I think you need to explain to us how the user selects where they want to go. The simplest way for you (the programmer) is for them to specify 2 numbers -- row and column.

Borrowing anduril462's diagram:

Code:`O| |`

-+-+-

| |X

-+-+-

| |

If on the other hand you're think each square has a single number:

Code:`1|2|3`

-+-+-

4|5|6

-+-+-

7|8|9

Or did you mean something else? - 01-27-2013xTonyLeo
I used a switch statement and for every case it would move approptiately for example case KEY_UP: and it would move the coordinate up.

Also how would i access the arrow within the game without a if statement as this in an interactive tictactoe. - 01-27-2013smokeyangel
What do you mean, "access the arrow"?

Just randomly, I think if I were implementing a tictactoe game I'd have keypresses of numbers map to the board like so:

Code:`7|8|9`

-+-+-

4|5|6

-+-+-

1|2|3

I know that's not what you asked, but I don't really know what you asked! - 01-27-2013anduril462
So you're using KEY_UP, KEY_DOWN, etc to move the cursor for positioning the X or O. Somehow you must know what square they are in, so they don't go too far left. That is, if you start in the middle square, and they hit KEY_LEFT 17 times, you only move left once, since that is the edge of the board. Somehow your program must know that, must know which of the 9 possible squares they are in. You need to track that somehow if you aren't already. Each of those 9 positions maps to a (y, x) coordinate for your board. Say you start each turn in the middle square. That is position (y, x) = (1, 1) in your board. The user hits KEY_UP, so subtract one from the y coordinate. Now you are in the top-middle square, position (y, x) = (0, 1). The hit KEY_RIGHT, so add one to the x coordinate. Now they are in the top-right square, (y, x) = (0, 2). Make sure you don't go below 0 or above 2 for either y or x, no matter how many times they hit a given arrow key.

What it boils down to, is you must find a way to figure out the (y, x) for each square, and when the user puts an X or O in that square, and you draw it on the screen, you must also mark it in your board. I've already give you one suggestion, mapping screen coordinates. I've just given you a second suggestion, starting from a known position (middle square), calculate the new (y, x) coordinates after every key press. There are a multitude of ways to do this, you are free to pick any one of them, but you must pick one and start implementing it, otherwise, I can do nothing more to help you. - 01-27-2013Adak
You find each square by knowing it's x and y coordinates, - that is absolutely correct. Using this logic, you MUST have this known.

The rest of what you need you get by simply using your cursor location function(s): ( in Windows: getConsoleCursorPosition(), in DOS or Windows using conio.h: wherex() and wherey() ).

So you have an exact layout or map, to every square. As the cursor is moved by pressing the arrow keys, you call your cursor location function(s). You COULD add and subtract the coordinates of the cursor by moving from a known position as suggested above - but you don't have to. The program can tell you this information, and with Tic-Tac-Toe, it's not a hit on the run-time. LOL! - 01-27-2013anduril462
I don't know whether the OP is using Windows or some other OS, but they did state they are using curses for screen manipulation, so the function they are looking for is getyx().