Problem 1 |
public void letterGrid()
{
String[][] letters = new String[3][4];
for(int r=0; r<3; r++)
{
for(int c=0; c<4; c++)
{
System.out.print("Enter a letter: ");
letters[r][c]=in.next();
}
}
System.out.println();
for(int r=0; r<3; r++)
{
for(int c=0; c<4; c++)
{
System.out.print(letters[r][c]+" ");
}
System.out.println();
}
System.out.println();
System.out.println("Switched...");
System.out.println();
for(int r=0; r<4; r++)
{
for(int c=0; c<3; c++)
{
System.out.print(letters[c][r]+" ");
}
System.out.println();
}
} |
Problem3 |
public void assignNumbersToGrid()
{
int[][] num = new int[4][3];
int n=0,r,c;
boolean empty_slot;
for(int i=1; i<=12; i++)
{
System.out.print("Enter number "+i+": ");
n=in.nextInt();
empty_slot=false;
do{
//randomly choose a row and column index
r=(int)(Math.random()*4+0);
c=(int)(Math.random()*3+0);
//check to see if that slot in the array is empty
if(num[r][c]==0)
{
num[r][c]=n;
empty_slot=true;
}
}while(empty_slot==false);//if the slot was not empty choose a new slot
}
System.out.println();
System.out.println("The assigned places for the numbers entered are:");
for(int row=0; row<=3; row++)
{
for(int column=0; column<=2; column++)
{
System.out.print(num[row][column]+" ");
}
System.out.println();
}
} |
Problem 5 |
public void tictactoe()
{
String[][] spot = new String[4][4];
int row,column,turn,turns_taken=0;
boolean empty_spot,winner=false,computer_wins=false,human_wins=false;
boolean human_turn;
//fill array with empty spaces
for(int r=1; r<=3; r++)
for(int c=1; c<=3; c++)
spot[r][c]=" ";
do
{
System.out.print("Would you like to go first (1) or second (2): ");
turn=in.nextInt();
if(turn<1 | turn>2)
System.out.print("Invalid input.");
}while(turn<1 | turn>2);
if(turn==1)
human_turn=true;
else
human_turn=false;
do //while nobody has won
{
if(computer_wins==false && human_turn==true) //human turn
{
do
{
empty_spot=false;
do
{
System.out.print("Enter the row number (1-3): ");
row=in.nextInt();
if(row<1 | row>3)
System.out.println("Invalid row number.");
}
while(row<1 | row>3);
do
{
System.out.print("Enter the column number: (1-3) ");
column=in.nextInt();
if(column<1 | column>3)
System.out.println("Invalid column number.");
}
while(column<1 | column>3);
if(spot[row][column].equals(" "))
{
spot[row][column]="X";
empty_spot=true;
}
else
{
System.out.println("That spot is not available. Choose another.");
empty_spot=false;
}
}
while(empty_spot==false);
//print grid
for(int r=1; r<=3; r++)
{
for(int c=1; c<=3; c++)
System.out.print("["+spot[r][c]+"] ");
System.out.println();
}
//determine if human has won
if(spot[1][1].equals("X" ) && spot[1][2].equals("X") && spot[1][3].equals("X"))
human_wins=true;
else if(spot[2][1].equals("X" ) && spot[2][2].equals("X") && spot[2][3].equals("X"))
human_wins=true;
else if(spot[3][1].equals("X" ) && spot[3][2].equals("X") && spot[3][3].equals("X"))
human_wins=true;
else if(spot[1][1].equals("X" ) && spot[2][1].equals("X") && spot[3][1].equals("X"))
human_wins=true;
else if(spot[1][2].equals("X" ) && spot[2][2].equals("X") && spot[3][2].equals("X"))
human_wins=true;
else if(spot[1][3].equals("X" ) && spot[2][3].equals("X") && spot[3][3].equals("X"))
human_wins=true;
else if(spot[1][1].equals("X" ) && spot[2][2].equals("X") && spot[3][3].equals("X"))
human_wins=true;
else if(spot[1][3].equals("X" ) && spot[2][2].equals("X") && spot[3][1].equals("X"))
human_wins=true;
if(human_wins==true)
{
System.out.println();
System.out.println("Congratulations human. You are the winner.");
}
human_turn=false;//return turn to computer
turns_taken++;
}
else //computer turn
{
empty_spot=false;
do
{
row=(int)(Math.random()*3+1);
column=(int)(Math.random()*3+1);
if(spot[row][column].equals(" "))
{
spot[row][column]="O";
empty_spot=true;
}
}
while(empty_spot==false);
System.out.println();
System.out.println("I have selected a spot.");
//print the grid
for(int r=1; r<=3; r++)
{
for(int c=1; c<=3; c++)
System.out.print("["+spot[r][c]+"] ");
System.out.println();
}
System.out.println();
//check to see if the computer won
if(spot[1][1].equals("O" ) && spot[1][2].equals("O") && spot[1][3].equals("O"))
computer_wins=true;
else if(spot[2][1].equals("O" ) && spot[2][2].equals("O") && spot[2][3].equals("O"))
computer_wins=true;
else if(spot[3][1].equals("O" ) && spot[3][2].equals("O") && spot[3][3].equals("O"))
computer_wins=true;
else if(spot[1][1].equals("O" ) && spot[2][1].equals("O") && spot[3][1].equals("O"))
computer_wins=true;
else if(spot[1][2].equals("O" ) && spot[2][2].equals("O") && spot[3][2].equals("O"))
computer_wins=true;
else if(spot[1][3].equals("O" ) && spot[2][3].equals("O") && spot[3][3].equals("O"))
computer_wins=true;
else if(spot[1][1].equals("O" ) && spot[2][2].equals("O") && spot[3][3].equals("O"))
computer_wins=true;
else if(spot[1][3].equals("O" ) && spot[2][2].equals("O") && spot[3][1].equals("O"))
computer_wins=true;
if(computer_wins==true)
{
System.out.println();
System.out.println("I am the winner. No mere human can beat me.");
}
human_turn=true;//return turn to human
turns_taken++;
}
}
while(computer_wins==false && human_wins==false && turns_taken<9);
if(computer_wins==false && human_wins==false)
System.out.println("We have tied. This is a cat's game.");
} |
|
|
|