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

# Thread: Need help converting Pseudocode to java

1. ## 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;

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 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: ");
indexR = player - 1;
System.out.print ("Enter X column: ");
indexC = player - 1;
if (board [indexR][indexC] == "*"){
Set board[indexR][indexC] = "X";
}

//enter location of O
System.out.print("Enter O row");
indexR = player - 1;
System.out.print ("Enter O column: ");
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. ## 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.