Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.*;
- import java.io.FileWriter;
- import java.io.IOException;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- class Figure {
- public static int getLocationx() {
- String color = PlayerState.getPlayer();
- Scanner in = new Scanner(System.in);
- System.out.println("Enter y pos of choosen" + color + " circle: (1-5)");
- String s = in.nextLine();
- int num = Integer.parseInt(s);
- num--;
- if(num < 0 || num >= 5){
- System.out.println("Non existent step");
- getLocationx();
- }
- return num;
- }
- public static int getLocationy() {
- String color = PlayerState.getPlayer();
- Scanner in2 = new Scanner(System.in);
- System.out.println("Enter x pos of choosen "+ color +" circle: (1-4)");
- String s = in2.nextLine();
- int num2 = Integer.parseInt(s);
- num2--;
- if(num2 < 0 || num2 >= 6){
- System.out.println("Non existent step");
- getLocationy();
- }
- return num2;
- }
- public static String getDirection(){
- String color = PlayerState.getPlayer();
- Scanner in3 = new Scanner(System.in);
- System.out.println("Enter direction of choosen "+ color +" circle: (up, down, left, right)");
- String s = in3.nextLine();
- return s;
- }
- public static String getPlayerName(String playerNum){
- Scanner in4 = new Scanner(System.in);
- System.out.println(" Player " + playerNum + " Enter your name: ");
- String s = in4.nextLine();
- return s;
- }
- }
- class Table {
- public static String p(int x, int y, String tabla[][])
- {
- if(x < 0 || y < 0 || x >= 5 || y>=4)
- {
- return "X";
- }
- else
- {
- String asd = tabla[x][y];
- return asd;
- }
- }
- public static boolean checkTable(String tabla[][]){
- boolean victoryState = true;
- int foundPosB[][] = new int[4][2];
- int foundPosR[][] = new int[4][2];
- for(int i = 0; i < 5;i++)
- {
- for(int j = 0; j < 4;j++)
- {
- if(p(i,j,tabla) != "X" && p(i,j,tabla) == p(i,j + 1,tabla) && p(i,j,tabla) == p(i,j + 2,tabla))
- {
- victoryState = false;
- }
- if(p(i,j,tabla) != "X" && p(i,j,tabla) == p(i + 1,j,tabla) && p(i,j,tabla) == p(i + 2,j,tabla))
- {
- victoryState = false;
- }
- }
- }
- for(int i = 0; i < 5;i++)
- {
- for (int j = 0; j < 4; j++)
- {
- for(int d = -1; d<=1;d+=2)
- {
- if(p(i,j,tabla) != "X" && p(i,j,tabla) == p(i + 1 *d,j + 1,tabla) && p(i,j,tabla) == p(i + 2 * d,j + 2,tabla))
- {
- victoryState = false;
- }
- }
- }
- }
- return victoryState;
- }
- public static void showTable(String tabla[][])
- {
- System.out.print(" ");
- for(int l = 0; l < 4; l++)
- {
- System.out.print(l + 1 + " ");
- }
- System.out.print("\n");
- for(int i = 0; i < 5; i++)
- {
- System.out.print(i + 1 + " ");
- for(int j = 0; j < 4; j++)
- {
- System.out.print(tabla[i][j] + " ");
- }
- System.out.print("\n");
- }
- }
- public static String[][] setTable(String tabla[][], int locX, int locY, String direction, String player) {
- System.out.println("Generating table.. ");
- boolean runTil = true;
- do {
- if(tabla[locX][locY] == player) {
- if (direction.contains("up")) {
- if (locX - 1 < 0 || tabla[locX - 1][locY] == "B" || tabla[locX - 1][locY] == "R") {
- System.out.println("Up Step is invalid");
- direction = Figure.getDirection();
- runTil = true;
- } else {
- tabla[locX][locY] = "X";
- tabla[locX - 1][locY] = player;
- runTil = false;
- }
- }
- if (direction.contains("down")) {
- if (locX + 1 > 4 || tabla[locX + 1][locY] == "B" || tabla[locX + 1][locY] == "R") {
- System.out.println("Down Step is invalid");
- direction = Figure.getDirection();
- runTil = true;
- } else {
- tabla[locX][locY] = "X";
- tabla[locX + 1][locY] = player;
- runTil = false;
- }
- }
- if (direction.contains("left")) {
- if (locY - 1 < 0 || tabla[locX][locY - 1] == "B" || tabla[locX][locY - 1] == "R") {
- System.out.println("Left Step is invalid");
- direction = Figure.getDirection();
- runTil = true;
- } else {
- tabla[locX][locY] = "X";
- tabla[locX][locY - 1] = player;
- runTil = false;
- }
- }
- if (direction.contains("right")) {
- if (locY + 1 > 4 || tabla[locX][locY + 1] == "B" || tabla[locX][locY + 1] == "R") {
- System.out.println("Right Step is invalid");
- direction = Figure.getDirection();
- runTil = true;
- } else {
- tabla[locX][locY] = "X";
- tabla[locX][locY + 1] = player;
- runTil = false;
- }
- }
- }
- else
- {
- System.out.println("That's not your figure");
- locY = Figure.getLocationy();
- locX = Figure.getLocationx();
- }
- }while(runTil);
- return tabla;
- }
- }
- class Players{
- public static String player1(String playerNum){
- String name1 = Figure.getPlayerName(playerNum);
- return name1;
- }
- public static String player2(String playerNum){
- String name2 = Figure.getPlayerName(playerNum);
- return name2;
- }
- }
- class PlayerState{
- private static boolean playerState = true;
- private static String player = null;
- public static String State() {
- if (playerState) {
- player = "B";
- playerState = false;
- } else {
- player = "R";
- playerState = true;
- }
- return player;
- }
- public static String getPlayer(){
- return player;
- }
- }
- public class Circle {
- public static String[][] generateTable(){
- String tabla[][] = new String[5][4];
- for(int i = 0; i < 5; i++)
- {
- for(int j = 0; j < 4; j++)
- {
- tabla[i][j] = "X";
- }
- }
- for(int i = 0; i < 2; i++)
- {
- tabla[0][i * 2] = "B";
- tabla[0][i * 2 + 1] = "R";
- tabla[4][i * 2] = "R";
- tabla[4][i * 2 + 1] = "B";
- }
- return tabla;
- }
- public static void JsonWriter(String name1, String name2, int gamesWonByPlayer1, int gamesWonByPlayer2)
- {
- JSONObject playerDetails1 = new JSONObject();
- playerDetails1.put("Player1 name", name1);
- playerDetails1.put("Won games", gamesWonByPlayer1);
- JSONObject player1 = new JSONObject();
- player1.put("player1", playerDetails1);
- JSONObject playerDetails2 = new JSONObject();
- playerDetails2.put("Player1 name", name2);
- playerDetails2.put("Won games", gamesWonByPlayer2);
- JSONObject player2 = new JSONObject();
- player2.put("player2", playerDetails2);
- JSONArray Players = new JSONArray();
- Players.add(player1);
- Players.add(player2);
- try {
- FileWriter file = new FileWriter("names.json");
- file.write(Players.toJSONString());
- file.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static String JsonReader() {
- JSONParser jsonParser = new JSONParser();
- String wow = null;
- JSONArray PlayersList = null;
- try {
- FileReader reader = new FileReader("names.json");
- Object obj = jsonParser.parse(reader);
- PlayersList = (JSONArray) obj;
- //System.out.println(PlayersList);
- wow = PlayersList.toJSONString();
- //JSONObject PlayerObject = (JSONArray) PlayersList.get((Player1));
- //String Player = (String) PlayerObject.get("firstName");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return wow;
- }
- public static void main(String[] args) {
- String tabla[][] = generateTable(); //Tabla generalasa
- int gamesWonByPlayer1 = 0;
- int gamesWonByPlayer2 = 0;
- String player = null;
- String name1 = Players.player1("1");
- System.out.println("You are player one: (Blue) " + name1);
- String name2 = Players.player2("2");
- System.out.println("You are player two: (Red) " + name2);
- JsonWriter(name1, name2, gamesWonByPlayer1, gamesWonByPlayer2);
- String valami = JsonReader();
- System.out.println(valami);
- do
- {
- player = PlayerState.State();
- if(PlayerState.getPlayer() == "B")
- {
- System.out.println("Your turn " + name1);
- }
- if(PlayerState.getPlayer() == "R")
- {
- System.out.println("Your turn " + name2);
- }
- Table.showTable(tabla);
- int locY = Figure.getLocationy();
- int locX = Figure.getLocationx();
- String direction = Figure.getDirection();
- System.out.println(locY+1);
- System.out.println(locX+1);
- System.out.println(direction);
- tabla = Table.setTable(tabla, locX, locY, direction, player);
- }while(Table.checkTable(tabla));
- }
- private static void PlayerListObject(JSONObject emp) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement