Write a program that lets a user play the game of MovingDay. Here are the rules of the game:
- there are N Falses on N squares, M Trues on M squares, and one empty square for a total of N + M + 1 squares
- the Falses start out on the right, the Trues start out on the left, the empty square is in the middle
- there are two moves JUMP and SLIDE
- a False can JUMP over a True or False onto an empty square
- a True can JUMP over a False onto an empty square
- a False or True can slide onto an empty square
- Falses can ONLY move left, Trues can ONLY move right
- the user WINS when the Falses occupy the left most N squares, the Trues occupy the rightmost M squares, and the empty square is in the middle
- the user LOSES when the user can't make any more moves and the winning condition hasn't been satisfied
Sample run (bold indicates user input):
---- MovingDay Game ----
Number of Falses: 3
Number of Trues: 3
0 1 2 3 4 5 6
| T | T | T | | F | F | F |
Choose move: 4
Sliding False @ 4 to the LEFT
0 1 2 3 4 5 6
| T | T | T | F | | F | F |
Choose move: 2
Jumping True @ 2 to the RIGHT
0 1 2 3 4 5 6
| T | T | | F | T | F | F |
Choose move: 0
Cannot Jump True over a True!
Choose move: 1
Sliding True @ 1 to the RIGHT
0 1 2 3 4 5 6
| T | | T | F | T | F | F |
Choose move: 2
Cannot Slide True (no empty space).
Cannot Jump True (no empty space).
Choose move: 0
Sliding True @ 0 to the RIGHT
0 1 2 3 4 5 6
| | T | T | F | T | F | F |
Choose move: 0
Sliding True @ 0 to the RIGHT
Game Over! You lose!
---- MovingDay Game ----
Number of Falses: 3
Number of Trues: 3
0 1 2 3 4 5 6
| T | T | T | | F | F | F |
Choose move: 4
Sliding False @ 4 to the LEFT
... much later ...
0 1 2 3 4 5 6
| F | F | F | T | | T | T |
Choose move: 3
Sliding True @ 3 to the RIGHT
0 1 2 3 4 5 6
| F | F | F | | T | T | T |
Congratulations! You win!
The code is in Java. All the explanation is in the code comments. Hope this helps!
Code:
import java.util.*;
public class Main
{
// function to print the current state of Game
public static void printGame(int arr[]) {
for(int i=0; i<arr.length; i++)
System.out.print(i+" ");
System.out.println();
System.out.print("|");
for(int i=0; i<arr.length; i++) {
// true
if(arr[i] == 1)
System.out.print("T|");
// false
else if(arr[i] == 0)
System.out.print("F|");
// empty
else
System.out.print("|");
}
System.out.println();
}
public static void main(String[] args) {
// input stream
Scanner sc = new
Scanner(System.in);
// required variables
int f, t, n, move, emptyCell;
System.out.println("---- MovingDay
Game ----");
System.out.print("Number of Falses: ");
f = sc.nextInt();
System.out.print("Number of Trues: ");
t = sc.nextInt();
n = f + t + 1;
// make an array with trues at left, falses at right
// 0 -> false, 1 -> true, 2 -> empty
int arr[] = new int[n];
// trues
for(int i=0; i<t; i++ ) {
arr[i] = 1;
}
// middle element
arr[t] = 2;
emptyCell = t;
// falses
for(int i=t+1; i<n; i++ ) {
arr[i] = 0;
}
// start the game
while(true) {
// print the current state
printGame(arr);
// check for win situation
boolean winF = true;
for(int i=0; i<n; i++) {
// all falses should be at left
if(i < f && arr[i] != 0) {
winF = false;
break;
}
// middle should be empty
if(i == f && arr[i] != 2) {
winF = false;
break;
}
// all trues should be at right
if(i > f && arr[i] != 1) {
winF = false;
break;
}
}
if(winF) {
System.out.println("Congratulations! You win!");
break;
}
// check for losing condition
// no moves possible
// check for slides, true to right and false to left
if(!((emptyCell - 1) >= 0 && arr[emptyCell-1] == 1)
&& !((emptyCell + 1) < n && arr[emptyCell+1] ==
0)) {
// check for jumps
// for true
if( !((emptyCell - 2) >= 0 && arr[emptyCell-2] == 1
&& arr[emptyCell-1] == 0)
// for false
&& !((emptyCell + 2) < n && arr[emptyCell+2] ==
0)) {
System.out.println("Game Over! You lose!");
break;
}
}
// take valid input move
while(true) {
System.out.print("Choose move: ");
move = sc.nextInt();
// check for valid move
// for true
if(arr[move] == 1) {
// check to slide
if((move+1) == emptyCell) {
System.out.println("Sliding True @" + move + " to the
RIGHT");
// update game
arr[move] = 2;
arr[move+1] = 1;
emptyCell = move;
}
// else check for jump
else if((move+2) == emptyCell && arr[move+1] == 0) {
System.out.println("Jumping True @" + move + " to the
RIGHT");
// update game
arr[move] = 2;
arr[move+2] = 1;
emptyCell = move;
}
// invalid moves
else if((move+2) == emptyCell && arr[move+1] == 1) {
System.out.println("Cannot Jump True over a True!");
continue;
}
else {
System.out.println("Cannot Slide True (no empty space).");
System.out.println("Cannot Jump True (no empty space).");
continue;
}
break;
}
// for false
if(arr[move] == 0) {
// check to slide to left
if((move-1) == emptyCell) {
System.out.println("Sliding False @" + move + " to the
LEFT");
// update game
arr[move] = 2;
arr[move-1] = 0;
emptyCell = move;
}
// else check for jump
else if((move-2) == emptyCell) {
System.out.println("Jumping False @" + move + " to the
LEFT");
// update game
arr[move] = 2;
arr[move-2] = 1;
emptyCell = move;
}
// invalid move
else {
System.out.println("Cannot Slide False (no empty space).");
System.out.println("Cannot Jump False (no empty space).");
continue;
}
break;
}
}
}
}
}
Sample run:

Code screenshots:



Write a program that lets a user play the game of MovingDay. Here are the rules...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...
IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....
Write a program in the Codio programming environment that allows you to play the game of Rock / Paper / Scissors against the computer. Within the Codio starting project you will find starter code as well as tests to run at each stage. There are three stages to the program, as illustrated below. You must pass the tests at each stage before continuing in to the next stage. We may rerun all tests within Codio before grading your program. Please see...
In Java, Using arrays. You are going to write a program that will play a solitaire (one-player) game of Mancala (Links to an external site.) (Links to an external site.). The game of mancala consists of stones that are moved through various buckets towards a goal. In this version of mancala, the user will be able to choose a number of buckets and a number of stones with which to set up the game. The buckets will be created by...
(Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...
In python language
Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...
Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...
Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...
Write a program in python that lets the user play the game Rock, Paper, Scissors against the computer. The program should work as follows: When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. 2 corresponds to paper, and 3 corresponds to scissors. To set up the random number library, write the following at the top of your code: import random random.seed(300) Use...