In Java: I got most of the logic down but I'd like to seen an answer that actually prints out the T and H and moves them or those something with an image since I only print out the winner and responses with my own code.
Given below is the code for the question along with output. Please rate the answer if it helped. Thank you.
Hare.java
public class Hare {
private int position;
private int max_position;
public Hare(int max)
{
position = 1;
max_position = max;
}
public int getPosition()
{
return position;
}
private void move(int x)
{
position += x;
//make sure position is in range 1-max_position
if(position < 1)
position = 1;
else if(position > max_position)
position = max_position;
}
public void bigHop()
{
move(9);
}
public void smallHop()
{
move(1);
}
public void bigSlip()
{
move(-12);
}
public void smallSlip()
{
move(-2);
}
public boolean finishedRace()
{
return position == max_position;
}
}
Tortoise.java
public class Tortoise {
private int position;
private int max_position;
public Tortoise(int max)
{
position = 1;
max_position = max;
}
public int getPosition()
{
return position;
}
private void move(int x)
{
position += x;
//make sure position is in range 1-max_position
if(position < 1)
position = 1;
else if(position > max_position)
position = max_position;
}
public void fastPlod()
{
move(3);
}
public void slowPlod()
{
move(1);
}
public void slip()
{
move(-6);
}
public boolean finishedRace()
{
return position == max_position;
}
}
Race.java
import java.util.Random;
public class Race {
private static void printTrack(int tick, int length, Hare hare, Tortoise tortoise)
{
System.out.printf("\n Tick %3d: ", tick);
for( int i= 1; i <= length; i++)
{
if(i == hare.getPosition() && hare.getPosition() == tortoise.getPosition())
System.out.printf("[OUCH!!] ");
else if(i == hare.getPosition())
System.out.printf("[H]");
else if(i == tortoise.getPosition())
System.out.printf("[T]");
else
System.out.printf("[]");
}
System.out.printf(" \n");
}
public static void main(String[] args) {
int track_length = 50;
Hare hare = new Hare(track_length);
Tortoise tortoise = new Tortoise(track_length);
int tick = 1;
Random random = new Random(System.currentTimeMillis());
System.out.println("AND THEY'RE OFF!!");
printTrack(tick, track_length, hare, tortoise);
while(!hare.finishedRace() && !tortoise.finishedRace())
{
tick++;
int n = 1 + random.nextInt(10);
//generate move for hare
if(1 <= n && n <=2) //20 % big hop
hare.bigHop();
else if(3<=n && n <=5) //30% small hop
hare.smallHop();
else if(n == 6) //10% , big slip,
hare.bigSlip();
else if(7<=n && n<=8) //20% small slip
hare.smallSlip();
//no move if 9 and 10 , harre is asleep
//generate move for tortoise
if(1 <= n && n <=5) //50 % fast plod
tortoise.fastPlod();
else if(6<=n && n <=8) //30% slow plod
tortoise.slowPlod();
else //20% , slip
tortoise.slip();
printTrack(tick, track_length, hare, tortoise);
}
if(tortoise.finishedRace())
if(hare.finishedRace())
System.out.println("IT'S A TIE");
else
System.out.println("TORTOISE WINS!!");
else
System.out.println("HARE WINS!!");
}
}
output
AND THEY'RE OFF!!
Tick 1: [OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 2: [][][][T][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 3: [T][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 4: [H][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 5: [][H][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 6: [][][H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 7: [H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 8: [H][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 9: [][H][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 10: [H][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 11: [][][][][][][][][][OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 12: [][][][T][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 13: [T][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 14: [][][][T][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 15: [T][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 16: [][][][T][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 17: [H][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 18: [][][][][][][][T][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 19: [H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 20: [H][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 21: [][H][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 22: [][][][][][][][][][][H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 23: [][][][][][][][][H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 24: [H][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 25: [H][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 26: [H][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 27: [H][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 28: [OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 29: [OUCH!!] [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 30: [H][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 31: [][H][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 32: [H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 33: [][H][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 34: [H][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 35: [][][][][][][][][][H][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 36: [][][][][][][][][][][H][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 37: [][][][][][][][][][][][][][][][][][][T][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 38: [][][][][][][][][][][][][][][][][][H][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 39: [][][][][][][][][][][][][][][][][][][H][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 40: [][][][][][][H][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 41: [][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][]
Tick 42: [][][][][][H][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][]
Tick 43: [][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]
Tick 44: [][][][][][][][][][][][][][][H][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][][][]
Tick 45: [][][][][][][][][][][][][][][][H][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][]
Tick 46: [][][][][][][][][][][][][][H][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][]
Tick 47: [][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][]
Tick 48: [][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][]
Tick 49: [][][][][][][][][][][][][][][][][][][][][H][][][][][][T][][][][][][][][][][][][][][][][][][][][][][][]
Tick 50: [][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][T][][][][][][][][][][][][][][][][][][][][]
Tick 51: [][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]
Tick 52: [][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]
Tick 53: [][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][]
Tick 54: [][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][]
Tick 55: [][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]
Tick 56: [][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][]
Tick 57: [][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][]
Tick 58: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][]
Tick 59: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][]
Tick 60: [][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]
Tick 61: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][]
Tick 62: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]
Tick 63: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][]
Tick 64: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][][]
Tick 65: [][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]
Tick 66: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][]
Tick 67: [][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][]
Tick 68: [H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][]
Tick 69: [][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][]
Tick 70: [][H][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][]
Tick 71: [][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][]
Tick 72: [][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][]
Tick 73: [][][][][][][][][H][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][]
Tick 74: [][][][][][][H][][][][][][][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][]
Tick 75: [][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][]
Tick 76: [][][][][][][][][][][][][][][][H][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][][][][]
Tick 77: [][][][][][][][][][][][][][][][][H][][][][][][][][][][][][][][][T][][][][][][][][][][][][][][][][][][]
Tick 78: [][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][][][T][][][][][][][][][][][][][][][]
Tick 79: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][T][][][][][][][][][][][][]
Tick 80: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][T][][][][][][][][][]
Tick 81: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][][][][][T][][][][][][]
Tick 82: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][T][][][]
Tick 83: [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][H][][][T]
TORTOISE WINS!!
In Java: I got most of the logic down but I'd like to seen an answer...
Create a program to recreate the classic race between the hare and the tortoise. Each animal will move across the screen left to right over the course of 70 squares from 1 - 70 (each will have their own variable valued 1 - 70 and will print as an H or T) . Random numbers will determine the amount each animal will move forward or backward. You will write a program calls functions that uses pointers and pass by reference...
I need just homework#6, I post homework# 5 to understand how to do it ? Please do it and please don't copy the answer CSCE 1030 Homework 5 Due: 11:59 PM on Monday, April 17,2017 PROGRAM DESCRIPTION: In this C++ program, you will start building the software infrastructure that will allow you to play a simplified version of the class game of Minesweeper that will display to the screen. In Homework 6, you will continue the work begun here to...
I need help with my programming assignment. The language used should be java and the algorithm should use search trees so that you play against the computer and he chooses the best move. The tree should have all possibilities on the leaves and you could use recursion to so that it populates itself. The game can be a 3*3 board (no need the make it n*n). Please put comments so that I can understand it. Thanks The game of ‘Walls’...
INTRODUCTION: Play sorry with two die, and one peg. Move your piece down the board first. But rolling certain numbers will change the pace of the game. INCLUDE IN YOUR ASSIGNMENT: Annotation is a major part of any program. At the top of each of your C++ programs, you should have at least four lines of documentation: // Program name: tictactoe.cpp // Author: Twilight Sparkle // Date last updated: 5/26/2016 // Purpose: Play the game of Tic-Tac-Toe ASSIGNMENT: Sorry Game...
I have already finished most of this assignment. I just need some help with the canMove and main methods; pseudocode is also acceptable. Also, the programming language is java. Crickets and Grasshoppers is a simple two player game played on a strip of n spaces. Each space can be empty or have a piece. The first player plays as the cricket pieces and the other plays as grasshoppers and the turns alternate. We’ll represent crickets as C and grasshoppers as...
Java question for two classes: Given the upper limit n as a parameter, the result of both methods is a boolean[] array of n elements that reveals the answers to that problem for all natural numbers below n in one swoop. public static boolean[] sumOfTwoDistinctSquares(int n) Determines which natural numbers can be expressed in the form a 2 + b 2 so that a and b are two distinct positive integers. In the boolean array returned as result, the i...
(Intro to Java help?) Define a class named RandomWalker. A RandomWalker object should keep track of its (x, y) location. All walkers start at the coordinates (0, 0). When a walker is asked to move, it randomly moves either left, right, up or down. Each of these four moves should occur with equal probability. The resulting behavior is known as a "random walk." (A 2-dimensional random walk example is pictured at right.) Each RandomWalker object should have the following public...
Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...
Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end show the exact Output that's shown in the Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 { public static void main( String [] args ) { //N denoting the size of the board int...
Must be done in Java. PROBLEM 1 INFORMATION AND THE CODE PROVIDED WITH IT AS WELL. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. At the end, show the exact Output that's shown in Problem 2. CODE PROVIDED FOR PROBLEM 1: import java.util.Scanner; public class Problem1 { public static void main( String [] args ) { //N denoting the size of the board int n; ...