Advertisement
Guest User

Untitled

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