Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.googlecode.lanterna.SGR;
- import com.googlecode.lanterna.TerminalPosition;
- import com.googlecode.lanterna.graphics.TextGraphics;
- import com.googlecode.lanterna.input.KeyStroke;
- import com.googlecode.lanterna.input.KeyType;
- import com.googlecode.lanterna.screen.Screen;
- import com.googlecode.lanterna.screen.TerminalScreen;
- import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
- import com.googlecode.lanterna.terminal.Terminal;
- import java.io.File;
- import java.io.IOException;
- import java.util.Scanner;
- import static com.googlecode.lanterna.input.KeyType.Enter;
- public class Main {
- public static void wypiszBS(Scanner file, TextGraphics tG) {
- int col = 0;
- while (file.hasNext()) {
- tG.putString(0, col, file.nextLine());
- col++;
- }
- }
- public static void main(String[] args) throws IOException {
- Terminal terminal = new DefaultTerminalFactory().createTerminal();
- Screen screen = new TerminalScreen(terminal);
- TextGraphics tG = screen.newTextGraphics();
- screen.startScreen();
- Board board = new Board(10, 10);
- Board Sboard = new Board(10, 10);
- Scanner file = new Scanner(new File("Napis.txt"));
- Game Game = new Game(board, Sboard);
- //Scoreboard scoreboard = new Scoreboard();
- int pom = 0;
- wypiszBS(file, tG);
- int index = 0, i;
- String[] xd = new String[]{
- "Start", "Topka", "Wyjście"
- };
- tG.putString(10, 10, "Wprowadz nazwe uzytkownika:", SGR.BOLD);
- screen.refresh();
- // tG.putString(10, 10, xd[0], SGR.BOLD);
- // tG.putString(10, 11, xd[1]);
- //
- //
- // terminal.flush();
- // terminal.setCursorVisible(false);
- // screen.refresh();
- boolean keepRunning = true;
- StringBuilder sb = new StringBuilder();
- int a = 0;
- while (keepRunning) {
- KeyStroke keyPresed = terminal.pollInput();
- if (keyPresed != null) {
- System.out.println(keyPresed);
- switch (keyPresed.getKeyType()) {
- case Backspace:
- tG.setCharacter(10,11, ' ');
- screen.refresh();
- case Character:
- a++;
- sb.append(keyPresed.getCharacter());
- tG.setCharacter(10 + a, 11, keyPresed.getCharacter());
- screen.refresh();
- break;
- // tG.putString(10, 10, xd[0], SGR.BOLD);
- // tG.putString(10, 11, xd[1]);
- // tG.putString(10, 12, xd[2]);
- // break;
- case ArrowDown:
- if (index == 2)
- break;
- tG.putString(10, 10 + index, xd[index]);
- screen.refresh();
- index++;
- tG.putString(10, 10 + index, xd[index], SGR.BOLD);
- screen.refresh();
- break;
- case ArrowUp:
- if (index == 0)
- break;
- tG.putString(10, 10 + index, xd[index]);
- screen.refresh();
- index--;
- tG.putString(10, 10 + index, xd[index], SGR.BOLD);
- screen.refresh();
- break;
- case Escape:
- keepRunning = false;
- default:
- System.out.println(keyPresed);
- case Enter:
- if (pom == 0) {
- screen.clear();
- wypiszBS(file, tG);
- screen.refresh();
- tG.putString(9, 9, "Elko" + " " + sb);
- tG.putString(10, 10, xd[0], SGR.BOLD);
- tG.putString(10, 11, xd[1]);
- tG.putString(10, 12, xd[2]);
- screen.refresh();
- pom++;
- break;
- }
- if (index == 0) {
- screen.clear();
- {
- Board.PrintTable(Sboard, tG);
- Game.SetShipsOnBoard(board);
- for (int z = 0; z < 10; z++) {
- System.out.println();
- for (int j = 0; j < 10; j++)
- System.out.print((char) board.getField(j, z) + " ");
- }
- /*for(int f = 0; f < 10; f++){
- for(int h = 0; h < 10; h++)
- Game.ShootField(f,h);
- }
- Game.ShootField(3,3);
- Board.PrintTable(Sboard,tG);*/
- }
- keepRunning = false;
- }
- if (index==1){
- screen.clear();
- screen.refresh();
- }
- if (index == 2) {
- screen.clear();
- i = 0;
- Scanner cya = new Scanner(new File("cya.txt"));
- while (cya.hasNext()) {
- tG.putString(0, i, cya.nextLine());
- i++;
- }
- screen.refresh();
- keepRunning = false;
- cya.close();
- }
- }
- }
- }
- int x = 1, y = 1;
- boolean gra = true;
- while (gra) {
- KeyStroke keyPresed = terminal.pollInput();
- screen.setCursorPosition(new TerminalPosition(x, y));
- screen.refresh();
- if (keyPresed != null) {
- switch (keyPresed.getKeyType()) {
- case ArrowRight:
- if (x == 10)
- break;
- x++;
- screen.setCursorPosition(new TerminalPosition(x, y));
- screen.refresh();
- break;
- case ArrowLeft:
- if (x == 1)
- break;
- x--;
- screen.setCursorPosition(new TerminalPosition(x, y));
- screen.refresh();
- break;
- case ArrowDown:
- if (y == 10)
- break;
- y++;
- screen.setCursorPosition(new TerminalPosition(x, y));
- //System.out.println(keyPresed);
- screen.refresh();
- break;
- case ArrowUp:
- if (y == 1)
- break;
- y--;
- screen.setCursorPosition(new TerminalPosition(x, y));
- // System.out.println(keyPresed);
- screen.refresh();
- break;
- case Enter:
- Game.ShootField(x - 1, y - 1);
- screen.refresh();
- Board.PrintTable(Sboard, tG);
- // int pts = Game.getPts();
- // int moves = Game.getMoves();
- //
- // System.out.println(pts + " " + moves);
- break;
- default:
- break;
- }
- }
- }
- screen.refresh();
- screen.readInput(); // readInput is blocking
- screen.stopScreen();
- file.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement