I have to write a code as a task for my university and we have to recreate Minesweeper with java and it has to be runned in the command line.
For the matchfield we have to make an array that looks in the end like this picture:
Example how it sould look in the end
And to choose the field we have to use the scanner.
For example if you want to chose field C3, you have to type into the scanner C3.
At the moment im struggleing a little bit with the field.
I had 2 ideas but both didn't work out very well.
in the first try i tried to create everything with 2 for loops and 1 array but my problem was that I couldn't add 2 charrs, so I had the chars 0 to 9 and the charrs A to J.
In the second try I created 3 array, one with the numbers 0 to 9 and anothe array A to J and in the third array i wanted to combine both arrays. And now I'm wondering if this it's possible if I can acctually combine them in the way I want and if it's possible could somebody give me some help?
import java.util.Scanner;
import java.util.Arrays;
public class Minesweeper {
public static void main (String[] args) {
char c = 'A';
char d = '0';
char e = '9';
char f = 'J';
char[][] feldz = new char[11][11];
char[][] feldb = new char[11][11];
char[][] feld = new char[11][11];
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (i == 0 && j == 0) {
feldz[i][j] = ' ';
System.out.print(feldz[i][j] + " |");
}
if (d > e) {
d = '0';
}
if (d <= e && i > 0){
feldz[i][j] = d;
System.out.print(feldz[i][j] + " |");
}
if (i > 0 && j == 10) {
d++;
}
}
System.out.println("");
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (i == 0 && j == 0) {
feldb[i][j] = ' ';
System.out.print(feldb[i][j] + " |");
}
if (i > 0 && j == 0){
feldb[i][j] = ' ';
System.out.print(feldb[i][j] + " |");
}
if (c > f) {
c = 'A';
}
if(c <= f && j > 0){
feldb[i][j] = c;
System.out.print(feldb[i][j] + " |");
c++;
}
if (j == 10){
System.out.println("");
}
}
}
}
}
You don't actually need array to print the maze , nested loops is enough for that. 2d Array is only required to store the input. Please try the below code:
int size = 10;
int [][] maze = new int[size][size];
while (true){
System.out.print(' ');
for (int i = 0; i < size; i++) {
System.out.print('|');
System.out.print((char) ('A' + i));
}
for (int i = 0; i < size; i++) {
System.out.println("");
System.out.print(i);
for (int j = 0; j < size; j++) {
System.out.print('|');
if(maze[i][j] > 0) {
System.out.print(maze[i][j]);
} else {
System.out.print(' ');
}
}
}
int row = -1;
int col = -1;
System.out.println("\nEnter CoOrdinates");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if(input.length() == 2) {
char charAt = input.charAt(0);
if(charAt >= 'A' && charAt <= 'A'+size-1) {
col = charAt-'A';
}
charAt = input.charAt(1);
if(charAt >= '0' && charAt <= '0'+size-1) {
row = charAt-'0';
}
if(row != -1 && col != -1) {
System.out.println("Enter Action");
input = scanner.nextLine();
int action = Integer.parseInt(input);
maze[row][col] = action;
} else {
System.out.println("Incorrect Input");
}
}
}
Related
I wrote a program that reads an array of integers and two numbers n and m. The program check that n and m never occur next to each other (in any order) in the array.
import java.util.*;
class Main {
public static void main(String[] args) {
// put your code here
Scanner scanner = new Scanner (System.in);
int len = scanner.nextInt();
int [] array = new int [len];
boolean broken = false;
for (int i = 0; i < len; i++){
array [i] = scanner.nextInt();
}
int n = scanner.nextInt();
int m = scanner.nextInt();
for (int j = 1; j < len; j++){
if((array[j]==n)&&(array[j+1]==m) || (array[j]==n)&&(array[j-1]==m) || (array[j]==m)&&(array[j+1]==n) || (array[j]==m)&&(array[j-1]==n)){
broken = true;
break;
}
}
System.out.println(broken);
}
}
Test input:
3
1 2 3
3 4
Correct output: true
My output is blank. What am I doing wrong?
Your code will throw ArrayIndexOutOfBoundsException as you are using array[j+1] whereas you have loop condition as j < len. The condition should be j < len -1.
The following works as expected:
for (int j = 1; j < len - 1; j++) {
if ((array[j] == n && array[j + 1] == m) || (array[j] == n && array[j - 1] == m)
|| (array[j] == m && array[j + 1] == n) || (array[j] == m && array[j - 1] == n)) {
broken = true;
break;
}
}
A sample run:
3
1 2 3
3 4
true
Your code will throw ArrayIndexOutOfBoundsException because of array[j+1] where j can be len-1. Actually you don't need to check both sides(previous and next element), checking combination with previous is enough since in the next iteration combination with next element will be checked.
for (int j = 1; j < len; j++){
if((array[j]==n && array[j-1]==m) || (array[j]==m && array[j-1]==n)){
broken = true;
break;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int len = scan.nextInt();
Map<Integer, Set<Integer>> map = new HashMap<>();
for (int i = 0, prv = 0; i < len; i++) {
int num = scan.nextInt();
if (!map.containsKey(num))
map.put(num, new HashSet<>());
if (i > 0)
map.get(prv).add(num);
prv = num;
}
int n = scan.nextInt();
int m = scan.nextInt();
boolean res = !map.containsKey(n) || !map.containsKey(m) || !map.get(n).contains(m) && !map.get(m).contains(n);
System.out.println(res);
}
So I want to skip the first and last elements of the array to initialize. What am I doing wrong?
public static void main(String args[]) throws Exception {
//Write code here
Scanner sc = new Scanner(System.in);
System.out.println("Input Rows: ");
int m = sc.nextInt();
System.out.println("Input Columns: ");
int n = sc.nextInt();
System.out.println("Enter values: ");
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] == arr[0][0] || arr[i][j] == arr[m][n]) {
continue;
} else {
arr[i][j] = sc.nextInt();
}
}
System.out.println();
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
Here is my output:
Input Rows:
3
Input Columns:
3
Entered Values:
0 0 0
0 0 0
0 0 0
You need to change the if condition inside the loop like following:
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if ((i == 0 && j==0) || (i == m -1 && j == n -1)) {
continue;
} else {
arr[i][j] = sc.nextInt();
}
}
System.out.println();
}
In this line:
if (arr[i][j] == arr[0][0] || arr[i][j] == arr[m][n]) {
You are testing for the equality of values within your array. You should be comparing whether the indices you are looking at are the beginning or end of the array.
That is to say, you want to compare whether (in pseudo code):
i==0 and j==0, OR i==max index in its dimension and j==max index in its dimension
I have deliberately omitted the literal answer, because this looks a tiny bit like homework.
You compare the value of arr[i][j] with the value of arr[0][0]. You should instead compare i==0 && j==0 || i==m -1 && j==n -1
As your array was empty, and as you start the loop, arr[i][j] was equal to arr[0][0], skipping the first element. but for the next loop, arr[i][j] was still empty, and as you compare it to a non-initialised value, it's always true, skipping in each step
public static void main(String args[]) throws Exception {
//Write code here
Scanner sc = new Scanner(System.in);
System.out.println("Input Rows: ");
int m = sc.nextInt();
System.out.println("Input Coloumns: ");
int n = sc.nextInt();
System.out.println("Enter values: ");
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i==0 && j==0 || i==m-1 && j==n-1) {
continue;
} else {
arr[i][j] = sc.nextInt();
}
}
System.out.println();
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
You don't need to check whether the array items are equal, you just want to check whether the row and column are equal to the last and first.
I've looked over lots of posts already and they have helped a lot, but none have covered my issue. I'm trying to print out an alternating checkerboard pattern for a class assignment. My output starting on the first line and every odd line has an extra print at the end. It should be repeating a 8x8 pattern basically. Here is my code and a screenshot of my output.
I need to know how to alter the code so that I only get 8 asterisks in the odd lines instead of the 9 that are showing now.
public class Checkerboard {
public static void main(String[] args) {
int length = 16;
int height = 8;
for (int i = 0; i <= height; i++)
{
if (i % 2 == 0)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
for (int j = 0; j <= length; j++)
{
if (j % 2 == 0)
{
System.out.print(" ");
}
else
{
System.out.print("* ");
}
}
System.out.println("");
}
}
}
output
This works for me. I changed the last else statement to an else if with the condition j != length || i % 2 != 0 so now if it is an odd number row it will not print out an extra '*' at the end.
public class mainTest {
public static void main(String[] args) {
int length = 15;
int height = 8;
for (int i = 0; i <= height; i++)
{
if (i % 2 == 0)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
for (int j = 0; j <= length; j++)
{
if (j % 2 == 0)
{
System.out.print(" ");
}
else if (j != length || i % 2 != 0)
{
System.out.print("* ");
}
}
System.out.println("");
}
}
}
Although below snippet is not optimized, but it should work for you. There is scope of simplification. Try that.
public static void main(String[] args) {
int length = 16;
int height = 8;
for (int i = 0; i <= height; i++) {
char first = ' ';
char second= '*';
if (i % 2 == 0) {
first = '*';
second = ' ';
}
for (int j = 0; j < length; j++) {
if (j % 2 == 0) {
System.out.print(first);
} else {
System.out.print(second);
}
}
System.out.println("");
}
}
At the moment, I have working code for a simple Tic Tac Toe program written in java. The only problem, as you'll see below, is that the null character (\u0000) is being printed instead of an open space when my board is displayed.
My professor told us to write this program in such a way that null spaces are detected and to fill them with either X or O, which I did.
Now, I would like to be able to change the null character from appearing as 00 to just a blank space since the format is incorrect otherwise.
I already tried simply erasing the '\u0000' character and replacing it with a ' ' character but then my board doesn't show up at all. Any help is appreciated!
import java.util.Scanner;
public class TicTacToe
{
public static void main(String[] args)
{
char[][] board = new char[3][3];
while (true) {
makeCompMove(board, 'X');
displayBoard(board);
if(isWon('X', board)) {
System.out.println("\n\nComputer won!");
System.exit(1);
}
else if (isDraw(board)) {
System.out.println("\n\nDraw Game! No winner");
System.exit(2);
}
makeAMove(board, 'O');
displayBoard(board);
if (isWon('O', board)) {
System.out.println("\n\nPlayer won!");
System.exit(3);
}
else if (isDraw(board)) {
System.out.println("\n\nDraw Game! No winner");
System.exit(4);
}
}
}
public static void displayBoard(char[][] board)
{
for(int k = 0; k < 3; k++)
{
for(int i = 0; i < 28; i++)
{
System.out.print("-");
}
System.out.println();
for(int j = 0; j < 3; j++)
{
System.out.print("|" + " " + board[k][j] + " ");
}
System.out.println("|");
}
for(int i = 0; i < 28; i++)
{
System.out.print("-");
}
}
public static void makeAMove(char[][] board, char o)
{
Scanner input = new Scanner(System.in);
while(true)
{
System.out.print("\n\nYour turn. Enter a row and col(0,1 or 2): ");
int row = input.nextInt();
int col = input.nextInt();
if(row > 2 || row < 0 || col > 2 || col < 0)
{
System.out.println("Incorrect Input. Try Again!");
continue;
}
if(board[row][col] == '\u0000')
{
System.out.print("\n You (O) have made your move...\n\n");
board[row][col] = 'O';
break;
}
else
System.out.println("Incorrect Input. Try Again!");
}
}
public static void makeCompMove(char[][] board, char x)
{
System.out.println();
System.out.println();
System.out.print("Computer (X) has made his move...\n");
while(true)
{
int row = (int)(Math.random()*3);
int col = (int)(Math.random()*3);
if(board[row][col] == '\u0000')
{
board[row][col] = x;
break;
}
}
System.out.println();
}
public static boolean isDraw(char[][] board)
{
for(int row = 0; row < 3; row++)
{
for(int col = 0; col < 3; col++)
{
if(board[row][col] == '\u0000')
{
return false;
}
}
}
return true;
}
public static boolean isWon(char x, char[][] board)
{
// Check Rows
for (int i = 0; i < 3; i++)
if (x == board[i][0] && x == board[i][1] && x == board[i][2])
return true;
// Check Columns
for (int j = 0; j < 3; j++)
if (x == board[0][j] && x == board[1][j] && x == board[2][j])
return true;
// Check first diagonal
if (x == board[0][0] && x == board[1][1] && x == board[2][2])
return true;
// Check second diagonal
if (x == board[0][2] && x == board[1][1] && x == board[2][0])
return true;
return false;
}
}
No need to change any of code You Just Check before display
in displayBoard use like this
for(int j = 0; j < 3; j++)
{
if(board[k][j]=='\u0000')
System.out.print("|" + " ");
else
System.out.print("|" + " " + board[k][j] + " ");
}
The elements in the two-dimensional array are set to the null character when the array is initialized. If you want to convert them all to a space, then iterate over them all and replace the character with a space.
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = ' ';
}
}
If the place is never used, then it will have a space instead of the null character in it. Do this before you use the array.
I am trying to extend the following code to sort the array if I added a third value 'C'. Would this be possible to do while retaining only one loop. The following code will sort an array with two possible values 'A' and 'B'.
public class TestSort
{
public static void main(String args[])
{
char f[] = {'A','B','B','A','B','B','A','B','A','A','B'};
int k = 0, t = f.length-1;
while(k < t)
{
if(f[k] == 'A')
k = k + 1;
else if(f[k] == 'B')
{
char m = f[t];
f[t] = f[k];
f[k] = m;
t = t - 1;
}
}
System.out.print("\nSorted List\n");
for(char i : f)
System.out.print(i + ", ");
System.out.println();
}
}
Here is an attempt. I don't know if I'm on the right track.
public class TestSort
{
static char f[] = {'C','A','B','A','C','B','A','B','C','C','B','A','B'};
//static char f[] = {'A','A','A','A','A','C','A','C','A','A','C','A','C'};
//static char f[] = {'C','B','B','B','C','B','B','B','C','C','B','C','B'};
//static char f[] = {'A','B','B','B','A','B','C','B','A','A','B','A','B'};
public static void main(String args[])
{
int j = 0, k = 0, t = f.length-1, l = f.length-1;
while(t >= 0)
{
if(f[k] == 'A')
k = k + 1;
else if(f[k] == 'B')
{
char m = f[j];
f[j] = f[k];
f[k] = m;
j = j + 1;
}
else if(f[k] == 'C')
{
char m = f[l];
f[l] = f[k];
f[k] = m;
l = l - 1;
}
for(char i : f)
System.out.print(i + ", ");
System.out.println();
}
}
}
Maybe something like:
public sort(char[] array) {
int[] frequencies = new int[3];
for(char c : array) {
if (c == 'A')
frequencies[0]++;
if (c == 'B')
frequencies[1]++;
if (c == 'C')
frequencies[2]++;
}
int index = 0;
for (int i = 0; i < frequencies[0]; i++) {
array[index++] = 'A';
}
for (int i = 0; i < frequencies[1]; i++) {
array[index++] = 'B';
}
for (int i = 0; i < frequencies[2]; i++) {
array[index++] = 'C';
}
}
Is the requirement "keep it O(n)", or "keep one loop" ?
Adding a second (non-nested) loop wouldn't change the O(n) quality. then you could do it in two steps: first push all the 'A's to the start and a second one to push all the 'C's to the end.