Advertisement
Guest User

Untitled

a guest
May 21st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.01 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.*;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import org.json.simple.JSONArray;
  6. import org.json.simple.JSONObject;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileReader;
  9. import org.json.simple.parser.JSONParser;
  10. import org.json.simple.parser.ParseException;
  11.  
  12.  
  13. class Figure {
  14.  
  15.  
  16. public static int getLocationx() {
  17. String color = PlayerState.getPlayer();
  18. Scanner in = new Scanner(System.in);
  19. System.out.println("Enter y pos of choosen" + color + " circle: (1-5)");
  20. String s = in.nextLine();
  21. int num = Integer.parseInt(s);
  22. num--;
  23. if(num < 0 || num >= 5){
  24. System.out.println("Non existent step");
  25. getLocationx();
  26. }
  27. return num;
  28. }
  29. public static int getLocationy() {
  30. String color = PlayerState.getPlayer();
  31. Scanner in2 = new Scanner(System.in);
  32. System.out.println("Enter x pos of choosen "+ color +" circle: (1-4)");
  33. String s = in2.nextLine();
  34. int num2 = Integer.parseInt(s);
  35. num2--;
  36. if(num2 < 0 || num2 >= 6){
  37. System.out.println("Non existent step");
  38. getLocationy();
  39. }
  40. return num2;
  41. }
  42. public static String getDirection(){
  43. String color = PlayerState.getPlayer();
  44. Scanner in3 = new Scanner(System.in);
  45. System.out.println("Enter direction of choosen "+ color +" circle: (up, down, left, right)");
  46. String s = in3.nextLine();
  47.  
  48. return s;
  49. }
  50. public static String getPlayerName(String playerNum){
  51. Scanner in4 = new Scanner(System.in);
  52. System.out.println(" Player " + playerNum + " Enter your name: ");
  53. String s = in4.nextLine();
  54.  
  55. return s;
  56. }
  57. }
  58.  
  59. class Table {
  60. public static String p(int x, int y, String tabla[][])
  61. {
  62. if(x < 0 || y < 0 || x >= 5 || y>=4)
  63. {
  64. return "X";
  65. }
  66. else
  67. {
  68. String asd = tabla[x][y];
  69. return asd;
  70. }
  71.  
  72. }
  73. public static boolean checkTable(String tabla[][]){
  74. boolean victoryState = true;
  75. int foundPosB[][] = new int[4][2];
  76. int foundPosR[][] = new int[4][2];
  77.  
  78. for(int i = 0; i < 5;i++)
  79. {
  80. for(int j = 0; j < 4;j++)
  81. {
  82. if(p(i,j,tabla) != "X" && p(i,j,tabla) == p(i,j + 1,tabla) && p(i,j,tabla) == p(i,j + 2,tabla))
  83. {
  84. victoryState = false;
  85. }
  86. if(p(i,j,tabla) != "X" && p(i,j,tabla) == p(i + 1,j,tabla) && p(i,j,tabla) == p(i + 2,j,tabla))
  87. {
  88. victoryState = false;
  89. }
  90. }
  91. }
  92. for(int i = 0; i < 5;i++)
  93. {
  94. for (int j = 0; j < 4; j++)
  95. {
  96. for(int d = -1; d<=1;d+=2)
  97. {
  98. 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))
  99. {
  100. victoryState = false;
  101. }
  102. }
  103. }
  104. }
  105. return victoryState;
  106. }
  107. public static void showTable(String tabla[][])
  108. {
  109. System.out.print(" ");
  110.  
  111. for(int l = 0; l < 4; l++)
  112. {
  113. System.out.print(l + 1 + " ");
  114. }
  115. System.out.print("\n");
  116.  
  117. for(int i = 0; i < 5; i++)
  118. {
  119. System.out.print(i + 1 + " ");
  120. for(int j = 0; j < 4; j++)
  121. {
  122. System.out.print(tabla[i][j] + " ");
  123. }
  124. System.out.print("\n");
  125. }
  126. }
  127. public static String[][] setTable(String tabla[][], int locX, int locY, String direction, String player) {
  128.  
  129. System.out.println("Generating table.. ");
  130.  
  131. boolean runTil = true;
  132.  
  133. do {
  134. if(tabla[locX][locY] == player) {
  135. if (direction.contains("up")) {
  136. if (locX - 1 < 0 || tabla[locX - 1][locY] == "B" || tabla[locX - 1][locY] == "R") {
  137. System.out.println("Up Step is invalid");
  138. direction = Figure.getDirection();
  139. runTil = true;
  140. } else {
  141. tabla[locX][locY] = "X";
  142. tabla[locX - 1][locY] = player;
  143. runTil = false;
  144. }
  145. }
  146. if (direction.contains("down")) {
  147. if (locX + 1 > 4 || tabla[locX + 1][locY] == "B" || tabla[locX + 1][locY] == "R") {
  148. System.out.println("Down Step is invalid");
  149. direction = Figure.getDirection();
  150. runTil = true;
  151. } else {
  152. tabla[locX][locY] = "X";
  153. tabla[locX + 1][locY] = player;
  154. runTil = false;
  155. }
  156. }
  157. if (direction.contains("left")) {
  158. if (locY - 1 < 0 || tabla[locX][locY - 1] == "B" || tabla[locX][locY - 1] == "R") {
  159. System.out.println("Left Step is invalid");
  160. direction = Figure.getDirection();
  161. runTil = true;
  162. } else {
  163. tabla[locX][locY] = "X";
  164. tabla[locX][locY - 1] = player;
  165. runTil = false;
  166. }
  167. }
  168. if (direction.contains("right")) {
  169. if (locY + 1 > 4 || tabla[locX][locY + 1] == "B" || tabla[locX][locY + 1] == "R") {
  170. System.out.println("Right Step is invalid");
  171. direction = Figure.getDirection();
  172. runTil = true;
  173. } else {
  174. tabla[locX][locY] = "X";
  175. tabla[locX][locY + 1] = player;
  176. runTil = false;
  177. }
  178. }
  179. }
  180. else
  181. {
  182. System.out.println("That's not your figure");
  183. locY = Figure.getLocationy();
  184. locX = Figure.getLocationx();
  185. }
  186. }while(runTil);
  187.  
  188. return tabla;
  189. }
  190. }
  191.  
  192. class Players{
  193.  
  194. public static String player1(String playerNum){
  195. String name1 = Figure.getPlayerName(playerNum);
  196. return name1;
  197. }
  198.  
  199. public static String player2(String playerNum){
  200. String name2 = Figure.getPlayerName(playerNum);
  201. return name2;
  202. }
  203.  
  204. }
  205.  
  206. class PlayerState{
  207.  
  208. private static boolean playerState = true;
  209. private static String player = null;
  210.  
  211. public static String State() {
  212.  
  213. if (playerState) {
  214. player = "B";
  215. playerState = false;
  216. } else {
  217. player = "R";
  218. playerState = true;
  219. }
  220. return player;
  221. }
  222. public static String getPlayer(){
  223. return player;
  224. }
  225. }
  226.  
  227.  
  228. public class Circle {
  229.  
  230. public static String[][] generateTable(){
  231. String tabla[][] = new String[5][4];
  232.  
  233. for(int i = 0; i < 5; i++)
  234. {
  235. for(int j = 0; j < 4; j++)
  236. {
  237. tabla[i][j] = "X";
  238. }
  239. }
  240.  
  241. for(int i = 0; i < 2; i++)
  242. {
  243. tabla[0][i * 2] = "B";
  244. tabla[0][i * 2 + 1] = "R";
  245. tabla[4][i * 2] = "R";
  246. tabla[4][i * 2 + 1] = "B";
  247. }
  248. return tabla;
  249. }
  250.  
  251. public static void JsonWriter(String name1, String name2, int gamesWonByPlayer1, int gamesWonByPlayer2)
  252. {
  253. JSONObject playerDetails1 = new JSONObject();
  254. playerDetails1.put("Player1 name", name1);
  255. playerDetails1.put("Won games", gamesWonByPlayer1);
  256.  
  257. JSONObject player1 = new JSONObject();
  258. player1.put("player1", playerDetails1);
  259.  
  260. JSONObject playerDetails2 = new JSONObject();
  261. playerDetails2.put("Player1 name", name2);
  262. playerDetails2.put("Won games", gamesWonByPlayer2);
  263.  
  264. JSONObject player2 = new JSONObject();
  265. player2.put("player2", playerDetails2);
  266.  
  267. JSONArray Players = new JSONArray();
  268. Players.add(player1);
  269. Players.add(player2);
  270.  
  271.  
  272.  
  273. try {
  274. FileWriter file = new FileWriter("names.json");
  275. file.write(Players.toJSONString());
  276. file.flush();
  277. } catch (IOException e) {
  278. e.printStackTrace();
  279. }
  280. }
  281.  
  282. public static String JsonReader() {
  283. JSONParser jsonParser = new JSONParser();
  284. String wow = null;
  285. JSONArray PlayersList = null;
  286.  
  287. try {
  288. FileReader reader = new FileReader("names.json");
  289.  
  290. Object obj = jsonParser.parse(reader);
  291.  
  292. PlayersList = (JSONArray) obj;
  293. //System.out.println(PlayersList);
  294. wow = PlayersList.toJSONString();
  295.  
  296. //JSONObject PlayerObject = (JSONArray) PlayersList.get((Player1));
  297.  
  298. //String Player = (String) PlayerObject.get("firstName");
  299.  
  300. } catch (FileNotFoundException e) {
  301. e.printStackTrace();
  302. } catch (ParseException e) {
  303. e.printStackTrace();
  304. } catch (IOException e) {
  305. e.printStackTrace();
  306. }
  307.  
  308.  
  309.  
  310. return wow;
  311. }
  312.  
  313. public static void main(String[] args) {
  314.  
  315. String tabla[][] = generateTable(); //Tabla generalasa
  316.  
  317. int gamesWonByPlayer1 = 0;
  318. int gamesWonByPlayer2 = 0;
  319.  
  320. String player = null;
  321.  
  322. String name1 = Players.player1("1");
  323. System.out.println("You are player one: (Blue) " + name1);
  324.  
  325. String name2 = Players.player2("2");
  326. System.out.println("You are player two: (Red) " + name2);
  327.  
  328. JsonWriter(name1, name2, gamesWonByPlayer1, gamesWonByPlayer2);
  329.  
  330.  
  331. String valami = JsonReader();
  332. System.out.println(valami);
  333.  
  334. do
  335. {
  336.  
  337. player = PlayerState.State();
  338. if(PlayerState.getPlayer() == "B")
  339. {
  340. System.out.println("Your turn " + name1);
  341. }
  342. if(PlayerState.getPlayer() == "R")
  343. {
  344. System.out.println("Your turn " + name2);
  345. }
  346.  
  347.  
  348.  
  349. Table.showTable(tabla);
  350.  
  351. int locY = Figure.getLocationy();
  352. int locX = Figure.getLocationx();
  353. String direction = Figure.getDirection();
  354.  
  355. System.out.println(locY+1);
  356. System.out.println(locX+1);
  357. System.out.println(direction);
  358.  
  359. tabla = Table.setTable(tabla, locX, locY, direction, player);
  360. }while(Table.checkTable(tabla));
  361.  
  362. }
  363.  
  364. private static void PlayerListObject(JSONObject emp) {
  365.  
  366. }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement