tic tac toe

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 604 Views - Last Post: 10 December 2017 - 02:26 PM Rate Topic: -----

#1 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 379
  • Joined: 22-October 13

tic tac toe

Posted 05 December 2017 - 06:14 PM

I am working on my tic tac toe game again but I am using c++ and text based gui. I am coding the computer player. here is the code I am working on. I am almost done. I am using a vector and random shuffle container to output a random computer player. I am trying to get the computer player to output a single "O" at a time. My code outputs s single "O" and then 2 "O"'s and then 3 "O"'s ... etc.
		for (vector<int>::iterator it = v_num.begin(); it != v_num.begin()+count; ++it)
		{
			o_num = *it;
			cout << ' ' << o_num;

			switch (o_num)
			{
			case 1:
				board[0][0] = 'O';
				flag_comp[0][0] = true;
				break;
			case 2:
				board[0][1] = 'O';
				flag_comp[0][1] = true;
				break;
			case 3:
				board[0][2] = 'O';
				flag_comp[0][2] = true;
				break;
			case 4:
				board[1][0] = 'O';
				flag_comp[1][0] = true;
				break;
			case 5:
				board[1][1] = 'O';
				flag_comp[1][1] = true;
				break;
			case 6:
				board[1][2] = 'O';
				flag_comp[1][2] = true;
				break;
			case 7:
				board[2][0] = 'O';
				flag_comp[2][0] = true;
				break;
			case 8:
				board[2][1] = 'O';
				flag_comp[2][1] = true;
				break;
			case 9:
				board[2][2] = 'O';
				flag_comp[2][2] = true;
				break;
			}
		}

my problem is with the for loop. I don't need any code just a programming hint.

Is This A Good Question/Topic? 0
  • +

Replies To: tic tac toe

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6111
  • View blog
  • Posts: 21,035
  • Joined: 05-May 12

Re: tic tac toe

Posted 05 December 2017 - 07:19 PM

The code you posted prints spaces and numbers, not letter O's. My programming hint is learn how to use a debugger so you can step through your code and see when the letter O's are showing up. Also choose a console font that makes it easier to distinguish between 0 vs. O.
Was This Post Helpful? 0
  • +
  • -

#3 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 379
  • Joined: 22-October 13

Re: tic tac toe

Posted 05 December 2017 - 07:26 PM

actually it prints characters but I having a problem with my vector and iterating through the vector, I am doing research on vectors.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6111
  • View blog
  • Posts: 21,035
  • Joined: 05-May 12

Re: tic tac toe

Posted 05 December 2017 - 07:44 PM

You have an iterator for a vector of integers. Unless you are ignoring a warning on line 3 about truncation data loss by assigning an integer into a char, then o_num must also be an int. That then means that line 4 will be printing a space followed by a number.

Only if o_num is a char would you print out a space followed by character, but that character would be a non-printing character if the expected values in the char match with any of the case values. In general ASCII values less than 32 are non-printing.
Was This Post Helpful? 0
  • +
  • -

#5 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 379
  • Joined: 22-October 13

Re: tic tac toe

Posted 05 December 2017 - 07:54 PM

I know I have a iterator for a vector of integers but I want to output only a single integer at a time not one then two then three values and so on.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6111
  • View blog
  • Posts: 21,035
  • Joined: 05-May 12

Re: tic tac toe

Posted 05 December 2017 - 09:05 PM

Back to using a debugger to figure out where the multiples are coming from. With the code that you presented above, it will output count integers separated by spaces. Something else is causing your program to output first 1, then 2, then 3 integers, etc. Perhaps you are calling the code above from someplace that has a loop and increments count.
Was This Post Helpful? 0
  • +
  • -

#7 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 379
  • Joined: 22-October 13

Re: tic tac toe

Posted 06 December 2017 - 03:27 PM

you are correct I am incrementing count using a while loop.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13954
  • View blog
  • Posts: 55,699
  • Joined: 12-June 08

Re: tic tac toe

Posted 06 December 2017 - 03:28 PM

Well.. knock that off and start to review your logic process again.
Was This Post Helpful? 0
  • +
  • -

#9 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 379
  • Joined: 22-October 13

Re: tic tac toe

Posted 06 December 2017 - 08:21 PM

I have pseudocoded my game to help me understand my logic process.
draw board
input X value
intialize vector with one to nine values
shuffle vector values
initialize array with X values
set X boolean value with true
iterate through vector values
set O values
intialize array with O value
set O boolean value with true
if boolean values are true
set array with X value
draw X and O values on the board
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6111
  • View blog
  • Posts: 21,035
  • Joined: 05-May 12

Re: tic tac toe

Posted 06 December 2017 - 11:42 PM

Huh? Perhaps it is because indentation is missing that is making it hard to understand that pseudo-code. Can you try again sharing, but this time out it in code tags and ident any logic that happens within a loop, or anything that only happens as a result of a branch?

Why do you have a separate array where you set X and O values, as then have another array for booleans?
Was This Post Helpful? 0
  • +
  • -

#11 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 379
  • Joined: 22-October 13

Re: tic tac toe

Posted 08 December 2017 - 04:22 PM

well here is my reworked pseudocode
draw board
initialize X values to zero
initialize O values to zero
initialize count to one
while count is less then or equal to nine
      input X value from player
      initialize vector
      input vector with values from one to nine
      shuffle vector values
   if X value is one to nine
      set board array with X value
      set flag array with true value
    use for loop to iterate through vector
    set iterate value to O value
  if O value is one to nine
      set board array with O value
      set flag_comp array with true value
   if flag array equals flag_comp array
     set board array to X value
 draw X value on board using board array
 draw O value on board using board array


Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7150
  • View blog
  • Posts: 14,893
  • Joined: 16-October 07

Re: tic tac toe

Posted 09 December 2017 - 01:55 PM

I'm afraid I'm not following your pseudo code. I'm also not sure why you're bothering: just write some actual code.

If you must, then:
-- too early?
---- draw board
-- this seems confusing
---- initialize X values to zero
---- initialize O values to zero
-- perhaps
initialize board, all positions empty
-- why? initialize count to one
-- oddly specific
---- while count is less then or equal to nine
while not done
    draw board
    if X turn
        -- ???
        ---- input X value from player
        ---- initialize vector
        ---- input vector with values from one to nine
        ---- shuffle vector values
        get X value from user
            until X value is valid
        update board X
    else
        computer pick available O value
        update board O
    -- no, draw once for each iteration, see the entry to the while
    ---- draw X value on board using board array
    ---- draw O value on board using board array
    done if has winner or no available moves
show final results
-- you might draw here



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#13 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 379
  • Joined: 22-October 13

Re: tic tac toe

Posted 09 December 2017 - 03:23 PM

well I finally got my computer player to work properly. YEAH!!!!!!! thanks for all the help!
Was This Post Helpful? 0
  • +
  • -

#14 pbivens85  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 379
  • Joined: 22-October 13

Re: tic tac toe

Posted 09 December 2017 - 04:58 PM

I have also adding scoring to my game.
Was This Post Helpful? 0
  • +
  • -

#15 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 780
  • View blog
  • Posts: 3,184
  • Joined: 13-June 14

Re: tic tac toe

Posted 09 December 2017 - 10:31 PM

Good work. Now go and learn about the minimax algorithm and have your computer player choose the best moves instead of random ones :).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2