Skip to content

Instantly share code, notes, and snippets.

@d630
Last active December 17, 2018 14:24
Show Gist options
  • Save d630/ef67657294b15ec10ddfe241ad73c3fa to your computer and use it in GitHub Desktop.
Save d630/ef67657294b15ec10ddfe241ad73c3fa to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class MatrixGame {
private char[][] mArray;
private int[] mGeo = new int[2];
// o, x, z, w
private char[] mChars = new char[4];
// tipp: 0, 1
// treffer: 2, 3
private int[] mCoords = new int[4];
private Scanner in = new Scanner(System.in);
private BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
private Random random = new Random();
// -- Constructors.
public MatrixGame() {
this.mChars = new char[] {'o', 'x', 'z', 'm'};
}
// -- Methods.
private boolean setMatrixGeo(int x, int y) {
if (x <= 0 || y <= 0) {
return false;
}
this.mGeo = new int[] {x, y};
return true;
}
private void setMatrixChars(char c, char d, char e, char f) {
this.mChars = new char[] {c, d, e, f};
}
private boolean setMatrixCoordsGuess(int[] a) {
if (a[0] > this.mGeo[0] || a[1] > this.mGeo[1] || a[0] <= 0 || a[1] <= 0) {
return false;
}
this.mCoords[0] = a[0] - 1;
this.mCoords[1] = a[1] - 1;
return true;
}
private void setMatrixCoordsMatch(int x, int y) {
this.mCoords[2] = x - 1;
this.mCoords[3] = y - 1;
}
private void setMatrixGuess() {
this.mArray[this.mCoords[0]][this.mCoords[1]] = this.mChars[1];
}
private void setMatrixEval() {
this.mArray[this.mCoords[2]][this.mCoords[3]] = this.mChars[2];
}
private void setMatrixMatch() {
this.mArray[this.mCoords[0]][this.mCoords[1]] = this.mChars[3];
}
private void prepareMatrix() {
for (int i = 0; i < this.mArray.length; i++) {
for (int j = 0; j < this.mArray[i].length; j++) {
this.mArray[i][j] = this.mChars[0];
}
}
}
private void createMatrix() {
this.mArray = new char[this.mGeo[0]][this.mGeo[1]];
}
private void printMatrix() {
for (int i = 0; i < this.mArray.length; i++) {
for (int j = 0; j < this.mArray[i].length; j++) {
System.out.print(this.mArray[i][j]);
}
System.out.println("");
}
System.out.println("");
}
private void resetChars() {
this.mArray[this.mCoords[0]][this.mCoords[1]] = this.mChars[0];
this.mArray[this.mCoords[2]][this.mCoords[3]] = this.mChars[0];
}
private boolean guess() {
// streams! :)
//see: https://stackoverflow.com/a/18838815
try {
if (this.setMatrixCoordsGuess(Arrays.stream(this.in2.readLine().trim().split("\\s*,\\s*")).mapToInt(Integer::parseInt).toArray())) {
this.setMatrixGuess();
return true;
}
} catch (IOException e) {
}
// }
return false;
}
private void eval() {
this.setMatrixCoordsMatch(
(this.random.nextInt((this.mGeo[0] - 1) + 1) + 1 ),
(this.random.nextInt((this.mGeo[1] - 1) + 1) + 1));
this.setMatrixEval();
}
private boolean isMatch() {
if ((this.mCoords[0] == this.mCoords[2] && this.mCoords[1] == this.mCoords[3])) {
setMatrixMatch();
return true;
}
return false;
}
public void play() {
int round = 1;
while (true) {
System.out.println("Round No" + round);
System.out.println("--------\n");
printMatrix();
do {
System.out.print("Bet on (int,int): ");
} while(! guess());
System.out.println("");
printMatrix();
eval();
if (isMatch()) {
printMatrix();
System.out.println("You win.");
System.exit(0);
} else {
printMatrix();
System.out.println("You loose.\n");
resetChars();
}
round++;
}
}
public void configure() {
int cols;
int lines;
int answer;
System.out.println("MatrixGame");
System.out.println("----------");
System.out.println("----------\n");
System.out.println("Configuration");
System.out.println("-------------");
do {
System.out.print("Lines: ");
lines = in.nextInt();
System.out.print("Cols: ");
cols = in.nextInt();
} while (! this.setMatrixGeo(lines, cols));
System.out.println("Using " + mGeo[0] + "," + mGeo[1]);
System.out.print("Do you wanna configure the default characters? (0/1) ");
answer = in.nextInt();
if (answer == 1) {
for (int i = 0; i < mChars.length; i++) {
System.out.print(mChars[i] + " => ");
// TODO
if (in.hasNext()) {
mChars[i] = in.next().charAt(0);
} else {
in.next();
}
}
}
System.out.print("Using ");
for (char e : mChars) {
System.out.print(e + " ");
}
System.out.println("\n");
this.createMatrix();
this.prepareMatrix();
}
// -- MAIN.
public static void main(String[] args) {
MatrixGame m = new MatrixGame();
m.configure();
m.play();
}
}
// vim: set ft=java :
@d630
Copy link
Author

d630 commented Apr 20, 2018

TODO Scanner/Reader verbessern

@d630
Copy link
Author

d630 commented Apr 21, 2018

TODO error handling

@PeterMueller5653
Copy link

change this to Exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment