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: Need help converting Pseudocode to java

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

    Default Need help converting Pseudocode to java

    // main module
    Module main()
    // Local variables
    Constant Integer ROWS = 3, COLS = 3
    Declare Integer indexR, indexC
    Declare winX = 0, winO = 0, player, draw
    Declare String board[ROWS][COLS], LineFeed = 10
     
    //initialize
    For indexR = 0 to ROWS – 1
    	For indexC = 0 to COLS – 1
    		Set board[indexR][indexC] = “*”
    	End For
    End For
     
    // game
    While winX == 0 and winO = 0
     
    // enter location for X
    	Display “enter X row: “
    	Input player
    	Set indexR = player - 1
    	Display “enter X column: “
    	Input player
    	Set indexC = player – 1
    	If board[indexR][indexC] == “*” Then
    		Set board[indexR][indexC] = “X”
    	End If
     
    	// enter location for O
    	Display “enter O row: “
    	Input player
    	Set indexR = player - 1
    	Display “enter O column: “
    	Input player
    	Set indexC = player – 1
    	If board[indexR][indexC] == “*” Then
    		Set board[indexR][indexC] = “O”
    	End If
     
    // check rows
    For indexR = 0 to ROWS – 1
    	If board[indexR][0] != “*” Then
    			If board[indexR][0] == board[indexR][1] Then 
    				If board[indexR][1] == board[indexR][2] Then
    					If board[indexR][0] == “X” Then
    						Set winX = 1
    					Else
    						Set winO = 1
    					End If
    				End If
    			End If
    		End If
    	End For
     
    // check columns
    For indexC = 0 to COLS – 1
    	If board[0][indexC] != “*” Then
    			If board[0][indexC] == board[1][indexC] Then 
    				If board[1][indexC] == board[2][indexC] Then
    					If board[0][indexC] == “X” Then
    						Set winX = 1
    					Else
    						Set winO = 1
    					End If
    				End If
    			End If
    		End If
    	End For
     
    // check diagonal
    	If board[0][0] != “*” Then
    			If board[0][0] == board[1][1] Then 
    				If board[1][1] == board[2][2] Then
    					If board[0][0] == “X” Then
    						Set winX = 1
    					Else
    						Set winO = 1
    					End If
    				End If
    			End If
    		End If
    	If board[0][2] != “*” Then
    			If board[0][2] == board[1][1] Then 
    				If board[1][1] == board[2][0] Then
    					If board[0][2] == “X” Then
    						Set winX = 1
    					Else
    						Set winO = 1
    					End If
    				End If
    			End If
    		End If
     
    //check if draw
    Set draw = 9
    For indexR = 0 to ROWS – 1
    		For indexC = 0 to COLS – 1
    			If board[indexR][indexC] == “*” Then
    				Set draw = 0
    			End If
    		End For
    End For
    If draw == 9 Then
    	Set winX = 1
    	Set winO = 1
    End If
     
    //display board
    For indexR = 0 to ROWS – 1
    		For indexC = 0 to COLS – 1
    			Display board[indexR][indexC]
    		End For
    		Display LineFeed
    End For
     
    	End While
     
     
    // show result
    If draw == 9 Then
    Display “Game is a draw.”
    	Else
    		If winX == 1 Then
    			Display “X wins!”
    		Else
    			Display “O wins!”
    		End If
    	End If
     
    End Module

    This is what I have so far.

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
        public class tictactoe {
               // Local variables
             private int indexR;
             private int indexC;
             private int winX = 0;
             private int winO = 0;
             private int player;
             private int draw;
             private BufferedReader reader = new BuffferedReader (new InputStreamReader(System.in));
             private char[][]board = new char [3][3];
             int LineFeed = 10;
     
                //initialize
             protected void init(){
                int counter = 0;
                for (indexR = 0; indexR < 3; indexR++){
                    for (indexC = 0; indexC < 3; indexC++){
                         board [indexR][indexC] = Character.forDigit(++counter, 10);
                     }
                  }
                winX = 1;
                player = 0;
             while (winX = 0; winO = 0){
     
             //Enter location for X
             System.out.print ("Enter X row: ");
             row = reader.readLine();
             indexR = player - 1;
             System.out.print ("Enter X column: ");
             column = reader.readLine();
             indexC = player - 1;
             if (board [indexR][indexC] == "*"){
                 Set board[indexR][indexC] = "X";
             }
     
             //enter location of O
             System.out.print("Enter O row");
             row = reader.readLine();
             indexR = player - 1;
             System.out.print ("Enter O column: ");
             column = reader.readLine();
             indexC = player - 1;
             if (board [indexR][indexC] == "*"){
                 Set board[indexR][indexC] = "O";
             }
     
             //check rows
             for (indexR = 0;indexR < 3; indexR++){
                 if (board[indexR][0] != "*"){
                     if (board[indexR][0] == board[indexR][1]){
                         if (board[indexR][1] == board[indexR][2]){
                             if (board[indexR][0] == "X"){
                                 winX = 1;
                             else
                                 winO = 1;
                            }
                        }
                    }
                }
            }
     
     
             //check columns
             for (indexC = 0; indexC<3;indexC++){
                 if (board[0][indexC] != "*"){
                     if (board[0][indexC] == board[1][indexC]){
                         if (board[1][indexC] == board[2][indexC]){
                             if (board[0][indexC] == "X"){
                                 winX = 1;
                             else
                                 winO = 1;
                            }
                        }
                    }
                }
            }
     
             //check diagonal
             if (board[0][0] != "*"){
                     if (board[0][0] == board[1][1]){
                         if (board[1][1] == board[2][2]){
                             if (board[0][0] == "X"){
                                 winX = 1;
                             else
                                 winO = 1;
                            }
                        }
                    }
                }
              if (board[0][2] != "*"){
                     if (board[0][2] == board[1][1]){
                         if (board[1][1] == board[2][0]){
                             if (board[0][2] == "X"){
                                 winX = 1;
                             else
                                 winO = 1;
                            }
                        }
                    }
                }
     
             //check if draw
             Set draw = 9;
             for (indexR = 0;indexR < 3; indexR++){
                 for (indexC =  0;indexC < 3; indexC++){
                     if (board [indexR] [indexC] == "*"){
                         Set draw = 0;
                     }
                 }
             }
             if (draw == 9){
                 winX = 1;
                 winO = 1;
             }
     
             //display board
             protected string drawBoard(){
                StringBuilder builder = new StringBuilder("*");
                for (int indexR = 0; indexR < 3; indexR++){
                    for (int indexC = 0; indexC < 3; indexC++){
                     System.out.print ([indexR][indexC]);
                 }
                 System.out.print (LineFeed);
             }
        }
     
         //show result
         if (draw == 9){
             System.out.print ("Game is a draw.");
          } else if (winX == 1){
                 System.out.print ("X wins!");
                } else {
                 System.out.print ("O wins!");
             }
         }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Need help converting Pseudocode to java

    Hello firebyrd. Welcome to the Java Programming Forums.

    Which part of this are you stuck on?
    Not many people will go through the Pseudocode and the code you have written trying to work out anything you have missed.

    Please break this down for us. The more information you give, the better replies you will receive.

    Thanks.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Replies: 4
    Last Post: February 23rd, 2011, 09:20 AM
  2. Converting Cases Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 27th, 2010, 12:07 PM
  3. Converting an array
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2010, 09:58 AM
  4. Replies: 0
    Last Post: December 3rd, 2009, 04:43 PM
  5. Replies: 4
    Last Post: May 1st, 2009, 03:32 PM