I started making this extremely simple 2048 game, without graphics and stuff (only array), and made the board :
import java.util.*;
public class GameBoard
{
String [][] GameBoard;
void justprint(String s, int n) //(ignore, just a function that makes next steps easier)
{
for(int i = 0; i<n;i++)
{
System.out.print(s);
}
System.out.println();
}
public GameBoard(int n)
{
GameBoard = new String [n][n];
Random num = new Random();
for(int i = 0; i<n;i++)
{
for(int j = 0; j<n;j++)
{
GameBoard[i][j] = " ";
}
}
System.out.print("-");
justprint("-----", n);
int r1 = num.nextInt(n);
int r2 = num.nextInt(n);
int r3 = num.nextInt(n);
int r4 = num.nextInt(n);
GameBoard[r1][r2] = " 2";
GameBoard[r3][r4] = " 2";
for(int i = 0; i<n;i++)
{
System.out.print("|");
for(int j = 0; j<n;j++)
{
System.out.print(GameBoard[i][j] + "|");
}
System.out.println();
System.out.print("-");
justprint("-----", n);
}
this.GameBoard = GameBoard;
}
}
Now, I want to make the functions to check if the neighbouring places are blank, or to add if they are the same number, etc.; but I have to make them in another class. I can probably make the functions to check all of that, but can someone tell me just how I could implement all of it in different classes?
I already tried doing it, but I want the user to input the size of the game board, and I can't do that as if I make a new class I have to write like
GameBoard gb = new GameBoard[4][4];
I don't want to define the size, but if I don't then I can't define my movement functions...
Any ideas?
Related
I'm trying to make a lottery game in java to run in the console afterwards with user input. I have a [3][9] array of random numbers between 1-9 in column 1, 10-19 in column 2, until 90, with half the numbers being 0, meaning they aren't part of the game or simply blanks.
So far, I have the numbers created in the array and they output fine, but I need to allow the user to have input and the numbers being guessed to start as blanks (or x instead of the number) and when the user actually gets the right number, it would switch that with the number generated previously. This would repeat itself until all the numbers were right, and then a message indicating a win would show.
How can I compare the inputs with the values generated? And how do I hide these values until they are guessed by the user?
Final Edit: If a line is completed with correct numbers, how do I keep track of this to also display message if this happens?
This is the random array index:
import java.util.Arrays;
import java.util.Random;
class Loto {
public static void main(String[] args) {
int[][] cartao = new int[3][9];
Random rand = new Random();
for(int i = 0; i< cartao.length; i++){
for(int j = 0; j< 5; j++){
int x = rand.nextInt(89) + 1;
while(cartao[i][x / 10] !=0) {
x = rand.nextInt(89) + 1;
}
cartao[i][x / 10] = x;
}
}
for(int[] row : cartao){
System.out.println(Arrays.toString(row));
}
}
}
You have to store what has been guessed correctly or not. For example you can use an auxiliary boolean matrix though it is not necessary (u can use only your card array, storing correctly guessed guesses has -1 for example), but it is easier to the eye I would say
public class Lotto {
boolean correctlyGuessed[][];
int lottoCard[][];
public Lotto() {
correctlyGuessed = new boolean[3][9];
lottoCard = new int[3][9];
Random rand = new Random();
for(int i = 0; i< lottoCard.length; i++){
for(int j = 0; j< 5; j++){
int x = rand.nextInt(89) + 1;
while(lottoCard[i][x / 10] !=0) {
x = rand.nextInt(89) + 1;
}
lottoCard[i][x / 10] = x;
}
}
}
public boolean guess(int row, int col, int number) {
if(lottoCard[row][col] == number) {
correctlyGuessed[row][col] = true;
return true;
}
return false;
}
public boolean hasLottoEnded() {
for(boolean[] arr:correctlyGuessed) {
for(boolean guess: arr) {
if(!guess) //if a guess is still false the game hasn't ended
return false;
}
}
return true;
}
public int getNumber(int row, int col) {
return lottoCard[row][col];
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
for(int row = 0; row < lottoCard.length; row++) {
for(int col = 0; col < lottoCard[row].length; col++) {
if(correctlyGuessed[row][col])
sb.append(lottoCard[row][col] + " ");
else
sb.append("X ");
}
//spacing
sb.append("\n");
}
return sb.toString();
}
public static void main(String[] args) {
Lotto card = new Lotto();
Scanner sc = new Scanner(System.in);
while(!card.hasLottoEnded()) { //loop whilst the game hasn't ended
System.out.println("Row:");
int row = sc.nextInt();
System.out.println("Column:");
int col = sc.nextInt();
System.out.println("Number:");
int number = sc.nextInt();
if(card.guess(row, col, number))
System.out.println("You have guessed correctly!");
else
System.out.println("Wrong guess :(");
System.out.println(card);
}
System.out.println("You win!");
sc.close();
}
}
The idea is each iteration will move the [x] every time it prints. However it obviously can't print a 0x0 matrix. As you can see I summoned the constructor in the program to have the variables 4, 4. Yet for some reason, after running it, the program still outputs 0,0.
public class MovingX{
private int rowsN;
private int columnsM;
private int[][] matrixArr = new int[rowsN][columnsM];
private int x = 0;
private int y = 0;
public MovingX(int n, int m){
n = rowsN;
m = columnsM;
}
public void forLoopGrid(){
for(int i = 0; i < matrixArr.length; i++){
for(int j = 0; j < matrixArr.length; j++){
if(i == x && j == y) System.out.print("[x] ");
else System.out.print("[ ] ");
}
System.out.println();
}
System.out.println();
}
public void moveX(){
if(x < rowsN) x++;
else{
x = 0; y++;
}
}
public void runProgram(){
System.out.println("Program Starting");
System.out.println("Rows, Columns: " + rowsN + " " + columnsM);
for(int i = 0; i < (rowsN * columnsM); i++){
System.out.println("Stuff happening");
forLoopGrid();
moveX();
}
System.out.println("Program Ending");
}
public static void main(String[] args){
MovingX xLoop = new MovingX(4, 4);
xLoop.runProgram();
}
}
The problem seems to be the fact that you are assigning your object's fields(rowsN,columnsM) to the arguments(n,m) supplied to your class constructor.
You see, it doesn't matter what arguments you supply during instantiation when you use those local constructor variables to store the default value of your object's fields. You get (0,0) because all object fields get initialized to a default value and for int, that happens to be 0.
This should work--
public MovingX(int n, int m){
rowsN=n; //n = rowsN;
columnsM=m; //m = columnsM;
}
Reason maybe with the assignment.
public MovingX(int n, int m){
rowsN = n;
columnsM = m;
}
Try this, they may work.
I am assigning 3 chars (two 'T's and one 'S') to a 10 x 10 grid. I have that down but I want to know how I could re randomize the char's positions if they end up on the same spot. Basically I am creating a little game where I am a soldier (S) and am on a grid with two Targets (T). Eventually I will have to eliminate the targets but I fist I want to make sure each one ends up in it's own spot. here is my code
import java.lang.Math;
public class PaintBall_Thomas{
public static void fillGrid(char[][] battleField){
for ( int i = 0; i < battleField.length; i++){
for ( int j = 0; j < battleField[i].length; j++){
battleField[i][j] = '-';
}
}
battleField[((int)(Math.random()*10))][((int)(Math.random()*10))] = 'S';//assigns a random number between 1-10 and assigns it to either soldier/target
battleField[((int)(Math.random()*10))][((int)(Math.random()*10))] = 'T';
battleField[((int)(Math.random()*10))][((int)(Math.random()*10))] = 'T';
for ( int i = 0; i < battleField.length; i++){
for ( int j = 0; j < battleField[i].length; j++){
System.out.print(battleField[i][j] + " ");
}
System.out.println();
}
}//end of fillGrid method
public static void main(String[] args) {
int columns = 10;
int rows = 10;
System.out.println("----------GRID----------");
//end of main
char[][] battleField = new char [rows][columns];
fillGrid(battleField);
}
}
this could be a good use for recursion, so like
public void addChar(char type) {
int row = (int)(Math.random() * battleField.length); // choose rand spot
int col = (int)(Math.random() * battleField.length);
if (battleField[row][col] == '-') { // if chosen space is free
battleField[row][col] = type; // add it
} else {
addChar(type) // try again if space was occupied
}
}
so this could have some problems... such as possibilities of getting a stack overflow if the board is already full... but if the method's only intended use is to add the two Ts and one S, it should be fine
so you could just add them like
addChar('T');
addChar('T');
addChar('S');
and none of the chosen spots will be duplicates
I am making a card game in Java and I have a Card object, a Deck (an array of card objects) and a Hand (Also an array of card objects).
I am using a method to draw cards in Deck which I have attached below:
public Card [] drawCard(Deck Deck , Hand Hand, int x, int y) {
Hand[x] = Deck[y];
return Hand;
}
In my main class I use the above method to draw cards from the Deck to the Hand.
for (i = 0 ; i < 2 ; i++) {
Deck.drawCard(Deck, PlayerHand, i, i);
}
for (i = 0 ; i < 2 ; i++) {
x = 2;
Deck.drawCard(Deck, PlayerHand, i, x);
x++;
}
String PlayerCard1 = PlayerHand[0];
String PlayerCard2 = PlayerHand[1];
String DealerCard1 = DealerHand[0];
String DealerCard2 = DealerHand[1];
However, I am receiving the error message "[Java] The type of the expression must be an array type but it resolved to Hand
Hand PlayerHand - Main.main(String[])".
I don't understand how to fix this error so can someone please explain to me where it's wrong?
I have attached my deck class and hand class below. Please feel free to give me feedback on them as well.
Hand class:
public class Hand {
Hand(){
Card [] Hand = new Card [9];
}
}
Deck class:
public class Deck {
//enter code here
Card [] Deck = new Card[52];
for(int i = 0; i < 4; i++) {
for(int x = 0; x < 13; x++) {
int cardNID = (i+1)*(x+1);
deck[cardNID] = new Card(i+1,x+1);
}
}
}
public void ShuffleDeck() {
for (int i = Deck.length - 1 ; i > 0; i = i - 1) {
int j = (int)(Math.random() * (i + 1));
int temp = myList[i];
myList[i] = myList[j];
myList[j] = temp;
}
}
public Card [] drawCard(Deck Deck , Hand Hand, int x, int y) {
Hand[x] = Deck[y];
return Hand;
}
}
This is my first question so I apologize if it isn't "good".
In this function:
public Card [] drawCard(Deck Deck , Hand Hand, int x, int y) {
Hand[x] = Deck[y];
return Hand;
You're passing objects not arrays. Then inside that functions you're trying to use them like they're arrays.
I'm working on a genetic algorithm which recreates paintings using triangles. At the moment my method used to populate children is changing the values of the parents and I can't figure out why.
public void populate()
{
System.out.println("pre: " + Arrays.toString(triangles[0][0]));
for(int drawNum = 2; drawNum < 12; drawNum ++)
{
for(int triNum = 0; triNum < numTriangles; triNum ++)
{
double pChooser = Math.random();
if(pChooser < .45)
{
triangles[drawNum][triNum] = triangles[0][triNum];
}
else if(pChooser < .9)
{
triangles[drawNum][triNum] = triangles[1][triNum];
}
else
{
triangles[drawNum][triNum] = randomTriangle();
}
}
}
genNum ++;
System.out.println("pst: " + Arrays.toString(triangles[0][0]));
}
private int[] randomTriangle()
{
int[] result = new int[9];
for(int i = 0; i < 9; i++)
{
result[i] = (int) (Math.random() * 256);
}
return result;
}
triangles is a 3 dimensional int array that holds each drawing, each triangle in the drawing, and each value of the endpoints and color of the triangle in the form triangles[drawingNumber][triangleNumber][values]. triangles[0] and triangles[1] are the two parents. The print statements print the values of the first triangle in the first parent before and after the children are formed. However, the pre and post print statements are returning different results, meaning that triangles[0][0] was changed somewhere in the code, but I can't figure out where that is.