Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA My output is not printing correctly. There should not be 2 instances of the same token on the board. Here is my JAVA code:

JAVA

image text in transcribed

My output is not printing correctly. There should not be 2 instances of the same token on the board.

Here is my JAVA code:

package Funky_Game;

public class FunkyGameDemo {

public static void main(String[] args) {

// TODO Auto-generated method stub

FunkyToken winner = null;

FunkyBoard board = new FunkyBoard(7);

FunkyToken[] tokens = new FunkyToken[3];

tokens[0] = new RandomToken('@');

tokens[1] = new MoveOneToken('$');

tokens[2] = new RandomToken('&');

board.placeToken(tokens[0], 3, 1);

board.placeToken(tokens[1], 4, 4);

board.placeToken(tokens[2], 0, 0);

board.displayBoard();

do {

for (int i = 0; i

if (tokens[i].active) {

tokens[i].move(board);

System.out.println(tokens[i].token + " move");

board.displayBoard();

winner = board.getWinner();

if (winner != null)

break;

}

}

} while (winner == null);

System.out.println(winner.token + " won!");

}

}

package Funky_Game;

public class FunkyBoard {

private FunkyToken[][] board;

public FunkyBoard(int size) {

board = new FunkyToken[size][size];

initializeBoard();

}

public FunkyToken[][] getBoard() {

return board;

}

public int getRows() {

return board.length;

}

public int getColumns() {

return board[0].length;

}

public void initializeBoard() {

for (int i = 0; i

for (int j = 0; j

board[i][j] = null;

}

}

}

public void displayBoard() {

for (FunkyToken[] row : board) {

System.out.print("|");

for (FunkyToken token : row) {

if (token != null) {

System.out.print(token.getToken() + "|");

} else {

System.out.print("-|");

}

}

System.out.println();

}

System.out.println();

}

public void placeToken(FunkyToken token, int row, int column) {

board[row][column] = token;

}

public FunkyToken getWinner() {

int activeCount = 0;

FunkyToken winner = null;

for (FunkyToken[] row : board) {

for (FunkyToken token : row) {

if (token != null && token.isActive()) {

activeCount++;

winner = token;

}

}

}

return activeCount == 1 ? winner : null;

}

} package Funky_Game;

public abstract class FunkyToken {

public char token;

public boolean active;

private int currentRow;

private int currentColumn;

public FunkyToken(char token) {

this.token = token;

this.active = true;

}

public char getToken() {

return token;

}

public boolean isActive() {

return active;

}

public int getCurrentRow() {

return currentRow;

}

public void setCurrentRow(int currentRow) {

this.currentRow = currentRow;

}

public int getCurrentColumn() {

return currentColumn;

}

public void setCurrentColumn(int currentColumn) {

this.currentColumn = currentColumn;

}

public abstract void move(FunkyBoard board);

}

package Funky_Game;

import java.util.Random;

public class MoveOneToken extends FunkyToken {

public MoveOneToken(char token) {

super(token);

}

public void move(FunkyBoard board) {

Random random = new Random();

int[] directions = {-1, 0, 1};

while (true) {

int newRow = getCurrentRow() + directions[random.nextInt(3)];

int newCol = getCurrentColumn() + directions[random.nextInt(3)];

if (isValidMove(board, newRow, newCol)) {

if (board.getBoard()[newRow][newCol] != null) {

}

board.placeToken(this, newRow, newCol);

board.placeToken(null, getCurrentRow(), getCurrentColumn());

setCurrentRow(newRow);

setCurrentColumn(newCol);

break;

}

}

}

private boolean isValidMove(FunkyBoard board, int row, int column) {

return row >= 0 && row = 0 && column

}

} package Funky_Game;

import java.util.Random;

public class RandomToken extends FunkyToken {

public RandomToken(char token) {

super(token);

}

public void move(FunkyBoard board) {

Random random = new Random();

while (true) {

int newRow = random.nextInt(board.getRows());

int newCol = random.nextInt(board.getColumns());

board.placeToken(this, newRow, newCol);

board.placeToken(null, getCurrentRow(), getCurrentColumn());

setCurrentRow(newRow);

setCurrentColumn(newCol);

break;

}

}

} Small portion of output: |&|-|-|-|-|-|-|

|-|-|-|-|-|-|-|

|-|-|-|-|-|-|-|

|-|@|-|-|-|-|-|

|-|-|-|-|$|-|-|

|-|-|-|-|-|-|-|

|-|-|-|-|-|-|-|

@ move

|-|-|-|-|-|@|-|

|-|-|-|-|-|-|-|

|-|-|-|-|-|-|-|

|-|@|-|-|-|-|-|

|-|-|-|-|$|-|-|

|-|-|-|-|-|-|-|

|-|-|-|-|-|-|-|

$ move

|-|$|-|-|-|@|-|

|-|-|-|-|-|-|-|

|-|-|-|-|-|-|-|

|-|@|-|-|-|-|-|

|-|-|-|-|$|-|-|

|-|-|-|-|-|-|-|

|-|-|-|-|-|-|-|

& move

|-|$|-|-|&|@|-|

|-|-|-|-|-|-|-|

|-|-|-|-|-|-|-|

|-|@|-|-|-|-|-|

|-|-|-|-|$|-|-|

|-|-|-|-|-|-|-|

|-|-|-|-|-|-|-|

@ move

|-|$|-|-|&|-|-|

|-|-|-|-|-|-|-|

|-|-|-|-|-|-|-|

|-|@|@|-|-|-|-|

|-|-|-|-|$|-|-|

|-|-|-|-|-|-|-|

|-|-|-|-|-|-|-|

Funky Board Game Design and implement a funky board game. (Please see the intro video.) Tokens will take turns to move on the board. (logic provided in the demo code) When there is only one token left on the board it wins the game. Create a game board. The size of the board can be passed in as a parameter. (Similar to TicTacToe Board in MP6) Create a FunkyToken class: - Users can pass in the token symbol they want to use. - All token pieces should be able to move. Create 2 sub classes: MoveOneToken and RandomToken. - MoveOneToken will move at a random direction, but will only move one space at a time. If the token is going to move out of the board, it will try a different direction and move again. If the spot already has a token, it will be replaced and removed from the board. - RandomToken will move to any random spot on the board. If the spot already has a token, it will be replaced and removed from the board. Use the provided FunkyGameDemo code to test run your program. Copy and paste the output of your program to a txt file and submit it with your source code just like the machine problems

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

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions

Question

Explain how humanistic therapists use the technique of reflection.

Answered: 1 week ago