Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. import com.googlecode.lanterna.SGR;
  2. import com.googlecode.lanterna.TerminalPosition;
  3. import com.googlecode.lanterna.TextColor;
  4. import com.googlecode.lanterna.graphics.TextGraphics;
  5. import com.googlecode.lanterna.input.KeyStroke;
  6. import com.googlecode.lanterna.screen.Screen;
  7. import com.googlecode.lanterna.screen.TerminalScreen;
  8. import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
  9. import com.googlecode.lanterna.terminal.Terminal;
  10.  
  11. import java.awt.*;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.Scanner;
  16.  
  17. import static com.googlecode.lanterna.input.KeyType.ArrowDown;
  18. import static com.googlecode.lanterna.input.KeyType.ArrowUp;
  19.  
  20. public class Main {
  21. private static Board board;
  22. private static Board Sboard;
  23.  
  24. private static int indexcap=0;
  25. private static int limit=999;
  26.  
  27. public static void main(String[] args) throws IOException {
  28. Terminal terminal = new DefaultTerminalFactory().createTerminal();
  29. Screen screen = new TerminalScreen(terminal);
  30.  
  31. TextGraphics tG = screen.newTextGraphics();
  32. screen.startScreen();
  33. int dimX = 0;
  34. int dimY= 0;
  35.  
  36. Scanner file = new Scanner(new File("Napis.txt"));
  37. //Scoreboard scoreboard = new Scoreboard();
  38.  
  39. int col = 0;
  40. while (file.hasNext()) {
  41. tG.putString(0, col, file.nextLine());
  42. col++;
  43. }
  44. file.close();
  45.  
  46. int index = 0, i;
  47. int x = 1, y = 1;
  48. ArrayList<String> xd = new ArrayList<String>();
  49. xd.add("Start");
  50. xd.add("Opcje");
  51. xd.add("Wyjście");
  52. tG.putString(10, 10, xd.get(0), SGR.BOLD);
  53. tG.putString(10, 11, xd.get(1));
  54. tG.putString(10, 12, xd.get(2));
  55.  
  56.  
  57. terminal.setCursorVisible(false);
  58. screen.refresh();
  59.  
  60. boolean keepRunning = true;
  61.  
  62. while (keepRunning) {
  63. KeyStroke keyPresed = terminal.pollInput();
  64.  
  65. if (keyPresed != null) {
  66. System.out.println(keyPresed);
  67. switch (keyPresed.getKeyType()) {
  68. case ArrowDown:
  69. if (index == xd.size() - 1)
  70. break;
  71.  
  72. tG.putString(10, 10 + index, xd.get(index));
  73. screen.refresh();
  74. index++;
  75.  
  76. tG.putString(10, 10 + index, xd.get(index), SGR.BOLD);
  77. screen.refresh();
  78. break;
  79.  
  80. case ArrowUp:
  81. if (index == indexcap)
  82. break;
  83. {
  84.  
  85. }
  86. tG.putString(10, 10 + index, xd.get(index));
  87. screen.refresh();
  88. index--;
  89. tG.putString(10, 10 + index, xd.get(index), SGR.BOLD);
  90. screen.refresh();
  91.  
  92. break;
  93. case Escape:
  94. index=0;
  95. indexcap=0;
  96. xd.remove(5);
  97. xd.remove(4);
  98. xd.remove(3);
  99. screen.clear();
  100. tG.putString(10, 10, xd.get(0), SGR.BOLD);
  101. tG.putString(10, 11, xd.get(1));
  102. tG.putString(10, 12, xd.get(2));
  103. screen.refresh();
  104. break;
  105.  
  106. default:
  107. System.out.println(keyPresed);
  108.  
  109. case Enter:
  110. if (index == 0) { //exit
  111. screen.clear();
  112. {
  113. System.out.println(dimX+ " "+dimY);
  114. Board board = new Board(dimX, dimY);
  115. Board sboard = new Board(dimX, dimY);
  116. Game Game = new Game(board, sboard);
  117. Board.PrintTable(board, tG);
  118. Game.SetShipsOnBoard();
  119. screen.refresh();
  120. }
  121. keepRunning = false;
  122.  
  123. }
  124. if (index == 2) {
  125. screen.clear();
  126. i = 0;
  127. Scanner cya = new Scanner(new File("cya.txt"));
  128. while (cya.hasNext()) {
  129. tG.putString(0, i, cya.nextLine());
  130. i++;
  131. }
  132. screen.refresh();
  133.  
  134. keepRunning = false;
  135.  
  136. cya.close();
  137. }
  138.  
  139. if (index == 1) //options
  140. {
  141. indexcap = 3;
  142. screen.clear();
  143. screen.refresh();
  144. xd.add("Łatwy(5 statków, tablica 10x10, nieograniczone ruchy)");
  145. xd.add("Średni(5 statków, tablica 12x12, maksymalnie 50 ruchów)");
  146. xd.add("Trudny(5 statków, tablica 12x12, maksymalnie 35 ruchów)");
  147. index = index + 2;
  148. screen.clear();
  149. screen.refresh();
  150.  
  151. if (index == 3) {
  152. dimX = 10;
  153. dimY = 10;
  154. limit = 999;
  155. }
  156. if (index == 4) {
  157. dimX = 12;
  158. dimY = 12;
  159. limit = 50;
  160. }
  161. if (index == 5) {
  162. dimX = 12;
  163. dimY = 12;
  164. limit = 50;
  165. }
  166. break;
  167.  
  168.  
  169. }
  170.  
  171.  
  172. }
  173. }
  174.  
  175. }
  176.  
  177. boolean gra = true;
  178. while(gra)
  179.  
  180. {
  181.  
  182. KeyStroke keyPresed = terminal.pollInput();
  183. screen.setCursorPosition(new TerminalPosition(x, y));
  184. screen.refresh();
  185.  
  186. if (keyPresed != null) {
  187. switch (keyPresed.getKeyType()) {
  188.  
  189. case ArrowRight:
  190. if (x == dimX)
  191. break;
  192. x++;
  193. screen.setCursorPosition(new TerminalPosition(x, y));
  194. System.out.println(keyPresed);
  195.  
  196. screen.refresh();
  197. break;
  198. case ArrowLeft:
  199. if (x == 1)
  200. break;
  201.  
  202. x--;
  203. screen.setCursorPosition(new TerminalPosition(x, y));
  204. System.out.println(keyPresed);
  205.  
  206. screen.refresh();
  207. break;
  208.  
  209.  
  210. case ArrowDown:
  211. if (y == dimY)
  212. break;
  213. y++;
  214. screen.setCursorPosition(new TerminalPosition(x, y));
  215. System.out.println(keyPresed);
  216. screen.refresh();
  217. break;
  218.  
  219.  
  220. case ArrowUp:
  221. if (y == 1)
  222. break;
  223.  
  224. y--;
  225. screen.setCursorPosition(new TerminalPosition(x, y));
  226. System.out.println(keyPresed);
  227. screen.refresh();
  228. break;
  229. case Enter:
  230. Game.ShootField(x - 1, y - 1);
  231. screen.refresh();
  232. Board.PrintTable(Sboard, tG);
  233. break;
  234. default:
  235. }
  236. }
  237. }
  238.  
  239.  
  240. screen.refresh();
  241. screen.readInput(); // readInput is blocking
  242.  
  243.  
  244. screen.stopScreen();
  245.  
  246.  
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement