Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

-------------question----- There will be a example code after question(not very right) Most two player games played using pieces are played on a board. But this

-------------question----- There will be a example code after question(not very right)

Most two player games played using pieces are played on a board. But this game is played in a single row instead! You will be implementing the game as a console-based Java program using Scanner.

Begin by asking how many spaces the row should be with a prompt using System.out.print like the following, with 40 as the maximally allowed number of spaces: (user input shown bold and underlined)

Please enter the number of spaces for the game (1-40): eight

To do this, use the promptNumberReadLine method that you will write, described on the back page. If the user does not type a number in the correct range, that method will prompt them in this way:

That was not a valid number! Please try again.

Please enter the number of spaces for the game (1-40): 8

Before each move, display the current state of the row by printing out the result of rowToString. The row begins as all empty spaces available for moves (an array full of zeros). The first player is represented by X and the second by O and an empty space by a period. Play always alternates between the two players. Each time a move is made, display the row using rowToString and ask the player which space to move in (1 through the number specified), also using the promptNumberReadLine method:

........

X, please enter your move (1-8):

If the number typed in is not an available space, print the following line and repeat the Each time a move is made procedure (which includes printing the board, prompting the user, and doing the check):

That space is not available! Please try again.

A move consists of the player placing the piece in the space indicated. If a move causes one or two lines (next to the left or right of placed piece) of opponent pieces to have no adjacent empty spaces, those pieces are removed. (The front and end of the row do not have empty spaces beyond them.) It is now the opponents turn, but the opponent is blocked from the spaces that were just cleared. After the opponents move, those spaces are available (but their move may cause other spaces to be blocked).

If the current player has no moves, the game is over! The winner is the player with the most pieces. You should print out the final board, who won, and allow the program to exit. If there is a tie, print the line:

Game over! Tie game!

If X won:

Game over! X wins.

The back side of the page describes the methods you must implement for this assignment. Each method begins with a description followed by the method declaration. The methods should be implemented inside a class called RowGame.

Print the prompt using System.out.print(). If the next piece of information in the Scanner represents an integer which is at least 1 and at most max, return the number, but make sure the Scanner also reads in the rest of the line before returning. Otherwise (if the next piece of information

doesnt represent the above requirements), read in the rest of the line, print the line

That was not a valid number! Please try again. and repeat the process described in this box.

public static int promptNumberReadLine(Scanner s, String prompt, int max)

Create and return a String that represents the game, all on one line. For each space, display X for a player one piece, O for player 2, . for available space, and - for a blocked space.

public static String rowToString(int[] game)

Given the game as argument, return 1 if the game is over and player 1 has won the game, 2 if player 2 has won, and 0 if the game is not yet over. (If the game is over with a tie, return -1.) public static int winner(int[] game)

If the given player can move to the given position (in the range 1 through game.length), modify the game array accordingly and return true. Otherwise, do not modify the game array and return false. public static boolean makeMove(int[] game, int player, int position)

Write a main method as specified on pg. 1 which uses the above methods to create a playable game. public static void main(String[] args)

You may use whatever numbers you wish in your int[] game array to represent different positions in the game, but the array will be initialized with 0 values by the tester for the first call to makeMove. The website BoardGameGeek has a description for this game under the name Alak.

Here are three example runs (two on the left and one on the right) where user keyboard input is shown bold and underlined. (Note that the (1-40): prompt is part of the first line but wrapped around.)

Please enter the number of spaces for the game (1-40): 4

....

X, please enter your move (1-4): 1 X...

O, please enter your move (1-4): 2

-O..

X, please enter your move (1-4): 4 .O.X

O, please enter your move (1-4): 3 .OO-

X, please enter your move (1-4): 1

XOO.

O, please enter your move (1-4): 4

XOOO

Game over! O wins.

Please enter the number of spaces for the game (1-40): 6

......

X, please enter your move (1-6): 2

.X....

O, please enter your move (1-6): 1

OX....

X, please enter your move (1-6): 3

OXX...

O, please enter your move (1-6): 6 OXX..O

X, please enter your move (1-6): 5

OXX.X-

O, please enter your move (1-6): 4

O--OX.

X, please enter your move (1-6): 2

That space is not available! Please try again.

O--OX.

X, please enter your move (1-6): 6 O..OXX

O, please enter your move (1-6): 0

That was not a valid number! Please try again.

O..OXX

O, please enter your move (1-6): 2 OO.OXX

X, please enter your move (1-6): 3

--X-XX

Game over! X wins.

Please enter the number of spaces for the game (1-40): 4

....

X, please enter your move (1-4): 2

.X..

O, please enter your move (1-4): 1

OX..

X, please enter your move (1-4): 4 OX.X

O, please enter your move (1-4): 3

O-O-

Game over! O wins.

import java.util.*;

public class RowGame {

public static void main(String[] args) {

Scanner scanner=new Scanner(System.in);

String pro="Please enter the number of spaces for the game (1-40): ";

int space=promptNumberReadLine(scanner,pro,40);

int[] game=new int[space];

System.out.println(rowToString(game));

int count=0;

int p1;

int p2;

String pp1="X, please enter your move (1-"+space+"): ";

String pp2="O, please enter your move (1-"+space+"): ";

int win=0;

while(win==0){

if(count%2==0){

p1=promptNumberReadLine(scanner,pp1,space);

while(!makeMove(game,1,p1)){

System.out.println("That space is not available! Please try again.");

p1=promptNumberReadLine(scanner,pp1,space);

}

System.out.println(rowToString(game));

}else{

p2=promptNumberReadLine(scanner,pp2,space);

while(!makeMove(game,2,p2)){

System.out.println("That space is not available! Please try again.");

p2=promptNumberReadLine(scanner,pp2,space);

}

System.out.println(rowToString(game));

}

count++;

win=winner(game);

}

if(win==1){

System.out.println("Game over! X wins.");

}else if(win==2){

System.out.println("Game over! O wins.");

}else if (win==-1){

System.out.println("Game over! Tie game!");

}

}

public static int promptNumberReadLine(Scanner s, String prompt, int max){

int num=1;

String n="";

do {

if(num < 1||num>max){

System.out.println("That was not a valid number! Please try again.");

}

System.out.print(prompt);

while (!s.hasNextInt()) {

s.nextLine();

System.out.println("That was not a valid number! Please try again.");

System.out.print(prompt);

}

n= s.nextLine();

while(!isNumber(n)){

System.out.println("That was not a valid number! Please try again.");

System.out.print(prompt);

n= s.nextLine();

}

int result = Integer.parseInt(n);

num =result;

}while (num < 1||num>max);

return num;

}

public static boolean isNumber(String str) {

int size = str.length();

for (int i = 0; i < size; i++) {

if (!Character.isDigit(str.charAt(i))) {

return false;

}

}

return size > 0;

}

public static String rowToString(int[] game){

String row="";

for(int i=0;i

if(game[i]==0){

row+=".";

}else if(game[i]==1){

row+="X";

}else if(game[i]==2){

row+="O";

}else{

row+="-";

}

}

return row;

}

public static int winner(int[] game){

int p1=0;

int p2=0;

int em=0;

for(int i=0;i

if(game[i]==1){

p1++;

} else if(game[i]==2){

p2++;

} else if (game[i]==0){

em++;

}

}

if(em==0){

if(p1>p2){

return 1;

}else if(p1

return 2;

}else{

return -1;

}

}else{

return 0;

}

}

public static boolean makeMove(int[] game, int player, int position){

for(int i=0;i

if(game[i]==3){

game[i]=0;

}

}

boolean move=true;

if(position>game.length||position<1){

return false;

}else{

if(game[position-1]==0){

game[position-1]=player;

if(position==2){

if(game[0]!=player&&game[0]!=0&&game[0]!=3){

game[0]=3;

}

}else if(position==game.length-1){

if(game[game.length-1]!=player&&game[game.length-1]!=0&&game[game.length-1]!=3){

game[game.length-1]=3;

}

}

int count=0;

for(int i=0;i

if(game[i]==1){

for(int j=i+1;j

if(game[j]==2){

if(game[j+1]==1){

game[j]=3;

}

}

}

}

}

for(int i=0;i

if(game[i]==2){

for(int j=i+1;j

if(game[j]==1){

if(game[j+1]==2){

game[j]=3;

}

}

}

}

}

return true;

}else{

return false;

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

107 MA ammeter 56 resistor ? V voltmeter

Answered: 1 week ago

Question

Generally If Drug A is an inducer of Drug B , Drug B levels will

Answered: 1 week ago