Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with tic tac toe java program?

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with tic tac toe java program?

    Can anyone help me figure out what's wrong with this program? I wrote the code but when i run it, no matter what number I input, it just says "Invalid marker" and then exits.



    import javax.swing.*;
    import java.util.*;

    public class tictactoe1
    {
    static String a,b,c,d,e,f,g,h,i;
    static int curPlay = 1;
    static int over = 0;
    public static void main(String[] args)

    {

    while (over != 1)
    {
    String square = displayBoard();
    int ava = checkSquare(square);
    if (ava == 1)
    {
    convertnumber(square);
    }
    checkwin(curPlay);
    switchPlayer(curPlay);
    rollComp();
    checkwin(curPlay);
    switchPlayer(curPlay);
    }
    }

    public static String displayBoard()
    {
    String number;
    a = "1";
    b = "2";
    c = "3";
    d = "4";
    e = "5";
    f = "6";
    g = "7";
    h = "8";
    i = "9";
    number = JOptionPane.showInputDialog(null, a + " " + b + " " + c + "\n" + d + " " + e + " " + f +"\n" + g + " " + h + " " + i + "\nEnter position that you would like to play");
    return number;
    }

    public static int checkSquare(String gameMarker)
    {
    System.out.println(a);

    int intMarker = Integer.parseInt(gameMarker);
    int ava = 0;
    String somenumber = " ";
    if (intMarker == 1)
    {
    somenumber = a;
    }
    else if(intMarker == 2)
    {
    somenumber = b;
    }
    else if(intMarker == 3)
    {
    somenumber = c;
    }
    else if(intMarker == 4)
    {
    somenumber = d;
    }
    else if(intMarker == 5)
    {
    somenumber = e;
    }
    else if(intMarker == 6)
    {
    somenumber = f;
    }
    else if(intMarker == 7)
    {
    somenumber = g;
    }
    else if(intMarker == 8)
    {
    somenumber = h;
    }
    else if(intMarker == 9)
    {
    somenumber = i;
    }

    if (somenumber.equals(intMarker))
    {
    ava = 1;
    }
    else
    {
    JOptionPane.showMessageDialog(null, "Invalid marker, try again");
    }
    return ava;

    }

    public static void convertnumber(String selection)
    {
    String str = " ";
    //String selection; //
    if(curPlay == 1)
    str = "X";
    else if(curPlay == 2)
    str = "O";
    if (selection.equals(a))
    a = str;
    else if (selection.equals(b))
    b = str;
    else if (selection.equals(c))
    c = str;
    else if (selection.equals(d))
    d = str;
    else if (selection.equals(e))
    e = str;
    else if (selection.equals(f))
    f = str;
    else if (selection.equals(g))
    g = str;
    else if (selection.equals(h))
    h = str;
    else if (selection.equals(i))
    i = str;
    else { }
    }






    public static void checkwin(int cPlayer)
    {
    if((a.equals(b) && b.equals(c)) || (d.equals(e) && e.equals(f)) || (g.equals(h) && h.equals(i)) || (a.equals(d) && d.equals(g)) || (b.equals(e) && e.equals(h)) || (c.equals(f) && f.equals(i)) || (a.equals(e) && e.equals(i)) || (c.equals(e) && e.equals(g)))
    {
    JOptionPane.showMessageDialog(null, " Player" + cPlayer + " wins! ");
    over = 1;
    }
    else if((!(a.equals(1))) && (!(b.equals(2))) && (!(c.equals(3))) && (!(d.equals(4))) && (!(e.equals(5))) && (!(f.equals(6))) && (!(g.equals(7))) && (!(h.equals(8))) && (!(i.equals(9))))
    {
    JOptionPane.showMessageDialog(null, "players have tied" + "\nbetter luck next time.");
    over = 1;
    }
    }





    public static int rollComp() //computer generates its choice
    {
    //String compWeapon = "";
    Random generator = new Random();
    double weaponNum = generator.nextDouble();
    int num = 0;
    while (num == 0)
    {

    if (weaponNum < (1/9))
    {
    a = "O";
    num = 1;
    }
    else if (weaponNum < (2 / 9))
    {
    b = "o";
    num = 2;
    }
    else if (weaponNum < (3 / 9))
    {
    c = "o";
    num = 3;
    }
    else if (weaponNum < (4 / 9))
    {
    d = "o";
    num = 4;
    }
    else if (weaponNum < (5 / 9))
    {
    e = "o";
    num = 5;
    }
    else if (weaponNum < (6 / 9))
    {
    f = "o";
    num = 6;
    }
    else if (weaponNum < (7 / 9))
    {
    g = "o";
    num = 7;
    }
    else if (weaponNum < (8 / 9))
    {
    h = "o";
    num = 8;
    }
    else if (weaponNum < (9 / 9))
    {
    i = "o";
    num = 9;
    }
    else { }
    }

    return num;
    }



    public static int switchPlayer(int gamenumber)
    {
    if (gamenumber == 0)
    {
    return 1;
    }
    else if (gamenumber == 1)
    {
    return 0;
    }
    else
    return 999;
    }


    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,542
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default Re: Help with tic tac toe java program?

    Help with tic tac toe java program?

    Duplicate post.
    Improving the world one idiot at a time!

Similar Threads

  1. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 06:32 AM
  2. Need help on Java program
    By middleonedb2001 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 1st, 2011, 09:42 PM
  3. My first Java Program
    By Creeeds in forum Java Theory & Questions
    Replies: 1
    Last Post: March 15th, 2011, 08:36 AM
  4. Help with this java program
    By Nemphiz in forum Object Oriented Programming
    Replies: 3
    Last Post: April 13th, 2010, 03:09 AM
  5. Convert Java Program to Java ME code
    By rinchan11 in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: October 5th, 2009, 10:18 PM