aspose file tools*
The moose likes Beginning Java and the fly likes tic tac toe and switch statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "tic tac toe and switch statement" Watch "tic tac toe and switch statement" New topic
Author

tic tac toe and switch statement

Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Hello guys,
I went to see my instructor today and resolved my problem with the
previous question. I know think I understand the logic of using flags. I'll
review it when I have some time, just to make sure it sticks in my head.
Anyways I have to make a tic tac toe game now for tomorrow. He explained to
me what I ought to do. But of course I am having problems implementing it in
java. First off, I think this is a basic tic tac toe game compared with
others I have seen on the net. Anyways the board is labeled like this;
123
456
789
I know I have to first count the number of x's and o's. For that I will use
two for loops (one for x's and one for o's). Then I need to set x equal to
y. IF there even, I know it is x's turn since x starts the game. If there
uneven its o's turn. My question is I know I have to count the number of x's
in a for loop. But do I just assign that to a char type, then after I count
both x's and o's I can compare them to see if there equal, if so its x's
move otherwise its o's move. Then I need to determine whether the moves are
valid or invalid. Then I need to implement a switch statement to account for
all the possible outcomes of the 9 possible causes. Any suggestions are most
welcome. Thanks again. I need to wrap my mind on the logic of implementing
these things in java.

so far what i got:

public class move {

public static void main(String[] args) {

String str = Stdin.readln();

char move;

// count the number of X's
int i = 0;
while(i<= str.length()){
i++;
System.out.println(i);
}
// count the number of O's
int j = 0;
while(j<= str.length()){
j++;
//System.out.println(j);
}

if(i == j){
move = 'x';
}else{
move = 'o';

System.out.println(move);
}










}
}
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Question: Why is this program printing out a O, when it should be an x? Here's my code updated code.

public class move {

public static void main(String[] args) {

String str = Stdin.readln();

boolean a = true;
char move;

// count the number of X's
int i = 0;
while(i<= str.length()){
i++;

//System.out.println(i);
}
// count the number of O's
int j = 0;
while(j<= str.length()){
j++;
//System.out.println(j);
}

a=(i%2==0 && j%2==0);
if(a){

move = 'X';
}else{
move = 'O';
}
System.out.println(move);











}
}
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
how do you do that? I mean setup the code so it is easier viewed... thanks again.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mike Smith:
how do you do that? I mean setup the code so it is easier viewed... thanks again.

When you fill out the form to post, there is a CODE button that inserts code tags...

http://faq.javaranch.com/view?UseCodeTags


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
sorry guys it doesn't seem to be working. Do i need to put the [code] [code] on every line of my code?
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
hello guys, sorry about the last 2 postings. I have now managed to solve the x problem. Now I know that i have to implement a switch statement to account for all the possible outcomes. Can anyone suggest a way to accomplish this. I understand if there are three consective x's or t consective o's the player wins. I figure my switch statement will deal with chars not integers. Am I wrong for thinking this?

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You need to take another look at your counting logic. This code is simply counting the number of chars in the String, without actually looking at what these chars are. So i and j will always be equal, and you will conclude that the number of x's equals the number of o's every time.

Basically, as you iterate through each char in the String, you need to test that char and determine whether it's an 'x' or an 'o', then increment some counter accordingly.

Looking ahead, I understand that you are labeling the board as follows:

123
456
789

So I'm assuming that this String you're working with is always going to be 9 chars, probably ordered 123456789. These will be x's and o's, along with some sort of place holder (like a space), right?
[ October 20, 2005: Message edited by: marc weber ]
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
hello, does this mean that I should use x and o as counters. That is the only thing i can think of.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mike Smith:
hello, does this mean that I should use x and o as counters. That is the only thing i can think of.

Try something like...

int xChars = 0;
int oChars = 0;

Then iterate through each char in the String and test it. If the char is an 'x', then increment xChars. If it's an 'o', then increment oChars.
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
hello, * is representing an empty spot. Basically the program should read in the following;

xoxoxo***7
winning

0***xx***4
valid

xoo*xx*xo1
invalid
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Ok, I am kind of unsure how to test each character. I would do the following to test though.

Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
I get no compile errors, but when i enter the xoxoxo's my computer doesn't respond in the console. Am I comparing wrong. I know how to compare strings, but characters I am a little confused. For strings you use equals() method;
anywhere have a look if you can. Thanks again
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Be careful with your variables. You need to be clear on what each one represents, and when it's approprate to modify it.

When you iterate over the chars in the String, you might use an int counter to keep track of where you are in the String. This has nothing to do with 'x' or 'o'. It's just an index that starts at the beginning and goes to the end. For example...

(A "for" loop might be better suited for this than a "while" loop. But either way...)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mike Smith:
I get no compile errors, but when i enter the xoxoxo's my computer doesn't respond in the console...

What exactly is Stdin? (I imagine this stands for "standard input," but what is it? Did your professor supply you with some sort of package for getting console input?)
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Ok,thanks guys, especially Mark. I think I now got that part of the code working with a for loop.But now is there a easy way to setup a switch statement. I know 3 x's in a row;win. Just as 3os in a row, win. SO i can omit the break statement in the switch statement to pass this correct?
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Stdin.readln(); is a method to collect input data. Its code is the following.
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Sorry, its a class(object) with methods.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Actually, I don't see how a switch statement makes much sense here. Basically, I see the next steps as:
  • Carefully define each of the 9 possible outcomes in terms of how the 9-character String would look.
  • Write tests for each of these outcomes.
  • Design your program flow to test as appropriate, and report the correct results.
  • I suppose you could define a variable like int outcome = 0; and then value this if any of the tests discover a win. For example, 1 might indicate a win across the top row, 2 a win across the middle row, etc. Then you could use a switch statement with this variable to report the result. For example, case 1: System.out.println("Top row win!"); break;

    :roll:
    [ October 20, 2005: Message edited by: marc weber ]
    Mike Smith
    Ranch Hand

    Joined: Sep 23, 2005
    Posts: 85
    Hi, when you speak of test each possible outcome do you mean if/else nested statements.Thanks again
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Mike Smith:
    Sorry, its a class(object) with methods.

    It's a class with a static readln method. And because the method is static, no object of that type needs to be created.

    In any case, that part is working fine. When you run the program, you won't see anything on the console. It will just sit there waiting for you to input a line that it can read. Just be sure to hit Enter to indicate the end of that line.
    [ October 21, 2005: Message edited by: marc weber ]
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Mike Smith:
    Hi, when you speak of test each possible outcome do you mean if/else nested statements.Thanks again

    That would work. Give it a try!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: tic tac toe and switch statement
     
    Similar Threads
    Game Tutorials -->> TicTacToe
    printing character array vertically with loop
    Turn Based Strategy Games
    TicTacToe game
    snakes and ladders game