├── .idea
├── .gitignore
├── description.html
├── project-template.xml
├── encodings.xml
├── modules.xml
├── misc.xml
└── uiDesigner.xml
├── out
└── production
│ └── tictactoec
│ └── novi
│ └── basics
│ ├── Main.class
│ ├── Board.class
│ ├── Field.class
│ ├── Player.class
│ └── tictactoe.class
├── src
└── novi
│ └── basics
│ ├── Main.java
│ ├── Field.java
│ ├── Player.java
│ ├── tictactoe.java
│ └── Board.java
└── tictactoec.iml
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/description.html:
--------------------------------------------------------------------------------
1 | Simple Java application that includes a class with main() method
--------------------------------------------------------------------------------
/.idea/project-template.xml:
--------------------------------------------------------------------------------
1 |
2 | IJ_BASE_PACKAGE
3 |
--------------------------------------------------------------------------------
/out/production/tictactoec/novi/basics/Main.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gitpeut/tictactoec/master/out/production/tictactoec/novi/basics/Main.class
--------------------------------------------------------------------------------
/out/production/tictactoec/novi/basics/Board.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gitpeut/tictactoec/master/out/production/tictactoec/novi/basics/Board.class
--------------------------------------------------------------------------------
/out/production/tictactoec/novi/basics/Field.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gitpeut/tictactoec/master/out/production/tictactoec/novi/basics/Field.class
--------------------------------------------------------------------------------
/out/production/tictactoec/novi/basics/Player.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gitpeut/tictactoec/master/out/production/tictactoec/novi/basics/Player.class
--------------------------------------------------------------------------------
/out/production/tictactoec/novi/basics/tictactoe.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gitpeut/tictactoec/master/out/production/tictactoec/novi/basics/tictactoe.class
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/novi/basics/Main.java:
--------------------------------------------------------------------------------
1 | package novi.basics;
2 |
3 | public class Main {
4 |
5 | public static void main(String[] args) {
6 | // write your code here
7 | tictactoe game = new tictactoe();
8 | game.play();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/tictactoec.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/novi/basics/Field.java:
--------------------------------------------------------------------------------
1 | package novi.basics;
2 |
3 | public class Field {
4 | private boolean empty;
5 | private Player owner;
6 | private char printToken;
7 |
8 | public Field() {
9 | empty = true;
10 | printToken = ' ';
11 | owner = null;
12 | }
13 |
14 | public void zero( char token){
15 | empty = true;
16 | owner = null;
17 | printToken = token;
18 | }
19 |
20 | public boolean occupy(Player player) {
21 |
22 | boolean return_value = false;
23 | if (empty) {
24 | empty = false;
25 | owner = player;
26 | printToken = player.getToken();
27 |
28 | return_value = true;
29 | }
30 | return return_value;
31 | }
32 |
33 | public boolean isEmpty() {
34 | return empty;
35 | }
36 |
37 | public char getToken() {
38 | return printToken;
39 | }
40 |
41 | public boolean setToken(char token) {
42 | if (empty) {
43 | printToken = token;
44 | return true;
45 | }
46 | return false;
47 | }
48 |
49 | public void setOwner(Player player) {
50 | owner = player;
51 | }
52 |
53 | public Player getOwner() {
54 | return owner;
55 | }
56 |
57 | }
--------------------------------------------------------------------------------
/src/novi/basics/Player.java:
--------------------------------------------------------------------------------
1 | package novi.basics;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Player {
6 |
7 | private char token;
8 | private int score;
9 | private boolean computer = false;
10 | private String name;
11 |
12 | public Player(){
13 | score = 0;
14 | }
15 |
16 | public String getName(){
17 | return name;
18 | }
19 | public int getScore(){
20 | return score;
21 | }
22 | public char getToken(){
23 | return token;
24 | }
25 | public boolean getComputer(){
26 | return computer;
27 | }
28 |
29 | public void setName( String name){
30 | this.name = name;
31 | }
32 | public void setScore( int score ){
33 | this.score += score ;
34 | }
35 | public boolean setToken( char token){
36 | this.token = token;
37 | return true;
38 | }
39 | public void setComputer( boolean computer){
40 | this.computer = computer;
41 | }
42 |
43 |
44 |
45 | public boolean askX() {
46 | Scanner input = new Scanner(System.in);
47 | boolean wantX = false;
48 |
49 | System.out.print("X starts. Do you want X ([Y]/n) ");
50 | String answer = input.nextLine();
51 |
52 | if ( answer.equals("") ){
53 | wantX = true;
54 | }else {
55 | switch (answer.charAt(0)) {
56 | case 'j':
57 | case 'y':
58 | case 'J':
59 | case 'Y':
60 | wantX = true;
61 | break;
62 | default:
63 | wantX = false;
64 | break;
65 | }
66 | }
67 |
68 | if( wantX ){
69 | token = 'X';
70 | }else{
71 | token = 'O';
72 | }
73 | return wantX;
74 |
75 | }
76 |
77 | public void askName() {
78 | Scanner input = new Scanner(System.in);
79 | boolean wantX = false;
80 |
81 | System.out.print("what is your name [you] ");
82 | String answer = input.nextLine();
83 |
84 | if ( answer.equals("") ){
85 | name = "you";
86 | }else {
87 | name = answer;
88 | }
89 | }
90 |
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/src/novi/basics/tictactoe.java:
--------------------------------------------------------------------------------
1 | package novi.basics;
2 |
3 | import java.util.Scanner;
4 |
5 | public class tictactoe {
6 |
7 | private Board board;
8 | private Player[] player;
9 | int playerIndex = 0;
10 |
11 | public tictactoe(){
12 | System.out.println("Tic, Tac and Toe");
13 | board = new Board();
14 | player = new Player[2];
15 |
16 | initPlayers();
17 |
18 | player[0].askName();
19 |
20 | if ( askComputerOpponent() ){
21 | player[1].setName( "computer");
22 | player[1].setComputer(true);
23 | }else{
24 | player[1].askName();
25 | }
26 | }
27 |
28 | private void initPlayers(){
29 | for ( int i =0 ; i < player.length; ++i) {
30 | player[i] = new Player();
31 | }
32 | }
33 |
34 |
35 | public void printScore(){
36 | for ( int i =0 ; i < player.length; ++i) {
37 | System.out.println(player[i].getName() + " score " + player[i].getScore());
38 | }
39 | }
40 |
41 | public void round(){
42 |
43 | if ( !player[0].askX() ){
44 | player[0].setToken('O');
45 | player[1].setToken('X');
46 | playerIndex = 1;
47 | }else{
48 | player[0].setToken('X');
49 | player[1].setToken('O');
50 | playerIndex = 0;
51 | }
52 |
53 | board.zero();
54 | board.print();
55 |
56 | while ( !board.checkWinner() ) {
57 |
58 | System.out.println( player[playerIndex].getName() + "'s turn: ");
59 |
60 | if ( player[playerIndex].getComputer() ){
61 | board.generateMove( player[playerIndex] );
62 | }else{
63 | board.askMove( player[playerIndex]);
64 | }
65 | board.print();
66 |
67 | playerIndex++;
68 | if ( playerIndex >= player.length) playerIndex = 0;
69 |
70 | }
71 |
72 | if ( board.winner != null ){
73 | board.winner.setScore(1);
74 | System.out.println("The winner is " + board.winner.getName() );
75 | }else{
76 | System.out.println("It's a draw, nobody won");
77 | }
78 |
79 | printScore();
80 | }
81 |
82 | public void play(){
83 | boolean gameOver = false;
84 |
85 | while ( !gameOver ){
86 |
87 | round();
88 |
89 | gameOver = !askAgain();
90 | if ( !gameOver ) board.zero();
91 | }
92 |
93 | System.out.println("Bye!");
94 |
95 |
96 | }
97 |
98 |
99 | public boolean askComputerOpponent() {
100 | Scanner input = new Scanner(System.in);
101 | boolean computer = false;
102 |
103 | System.out.print("Play against computer ([y]/n) ");
104 | String answer = input.nextLine();
105 |
106 | if ( answer.equals("") ){
107 | computer = true;
108 | }else {
109 | switch (answer.charAt(0)) {
110 | case 'j':
111 | case 'y':
112 | case 'J':
113 | case 'Y':
114 | computer = true;
115 | break;
116 | default:
117 | computer = false;
118 | break;
119 | }
120 | }
121 |
122 | return computer;
123 | }
124 |
125 | public boolean askAgain(){
126 | Scanner input = new Scanner( System.in );
127 | boolean again = false;
128 |
129 | System.out.print("Play again ? (y/[N]) ");
130 |
131 | String answer = input.nextLine();
132 | if ( answer.equals("")) return false;
133 |
134 | switch( answer.charAt(0) ){
135 | case 'j':
136 | case 'y':
137 | case 'J':
138 | case 'Y':
139 | again = true;
140 | break;
141 | default:
142 | break;
143 | }
144 |
145 | return again;
146 | }
147 |
148 |
149 |
150 | }
151 |
--------------------------------------------------------------------------------
/src/novi/basics/Board.java:
--------------------------------------------------------------------------------
1 | package novi.basics;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Board {
6 | Field[][] board = new Field[3][3];
7 | Player winner = null;
8 |
9 | public Board() {
10 | init();
11 | }
12 |
13 | private void init(){
14 | int number = 0;
15 | for (int v = 0; v < 3; ++v) {
16 | for (int h = 0; h < 3; ++h) {
17 | board[v][h] = new Field();
18 | }
19 | }
20 | }
21 |
22 | public boolean isFull() {
23 | for (int v = 0; v < 3; ++v) {
24 | for (int h = 0; h < 3; ++h) {
25 | if (board[v][h].isEmpty()) return false;
26 | }
27 | }
28 | return true;
29 | }
30 |
31 | public void print() {
32 | for (int v = 0; v < 3; ++v) {
33 | for (int h = 0; h < 3; ++h) {
34 | if (h > 0) System.out.print("|");
35 |
36 | char value = board[h][v].getToken();
37 | if (value == 0) value = ' ';
38 |
39 | System.out.print(" " + value + " ");
40 | }
41 | System.out.println();
42 | if (v < 2) System.out.println("----+-----+-----");
43 | }
44 | }
45 |
46 | public void zero() {
47 | winner = null;
48 |
49 | int number = 1;
50 | for (int v = 0; v < 3; ++v) {
51 | for (int h = 0; h < 3; ++h) {
52 | board[h][v].zero( (char) ('0' + number++) );
53 | }
54 | }
55 | }
56 |
57 | public boolean checkWinner() {
58 | if (isFull()) {
59 | winner = null;
60 | return true;
61 | }
62 |
63 | //check horizontals
64 | for (int v = 0; v < 3; ++v) {
65 | if (board[v][0].getOwner() == board[v][1].getOwner() &&
66 | board[v][0].getOwner() == board[v][2].getOwner() &&
67 | board[v][0].getOwner() != null) {
68 |
69 | winner = board[v][0].getOwner();
70 | return true;
71 | }
72 | }
73 | // check verticals
74 | for (int h = 0; h < 3; ++h) {
75 | if (board[0][h].getOwner() == board[1][h].getOwner() &&
76 | board[0][h].getOwner() == board[2][h].getOwner() &&
77 | board[0][h].getOwner() != null) {
78 | winner = board[0][h].getOwner();
79 | return true;
80 | }
81 | }
82 | // check diagonals
83 | if (board[0][0].getOwner() == board[1][1].getOwner() &&
84 | board[0][0].getOwner() == board[2][2].getOwner() &&
85 | board[1][1].getOwner() != null) {
86 | winner = board[0][0].getOwner();
87 | return true;
88 | }
89 |
90 | if (board[2][0].getOwner() == board[1][1].getOwner() &&
91 | board[2][0].getOwner() == board[0][2].getOwner() &&
92 | board[1][1].getOwner() != null) {
93 | winner = board[2][0].getOwner();
94 | return true;
95 | }
96 | return false;
97 | }
98 |
99 |
100 | public void generateMove( Player player) {
101 | boolean genValid = false;
102 |
103 | while (!genValid) {
104 | int x = (int) (Math.random() * (2.5));
105 | int y = (int) (Math.random() * (2.5));
106 |
107 | if (board[x][y].isEmpty() ) {
108 | genValid = true;
109 | board[x][y].occupy( player );
110 | }
111 | }
112 |
113 | }
114 |
115 | public void askMove( Player player){
116 | Scanner input = new Scanner( System.in );
117 | int number;
118 | boolean inputValid = false;
119 |
120 | while( !inputValid ) {
121 | System.out.print("Give number (1-9) of field to place your " + player.getToken() + " ");
122 | number = input.nextInt();
123 | if ( number < 1 || number > 9 ) {
124 | System.out.println("Invalid input");
125 | }else{
126 | inputValid = true;
127 | }
128 |
129 | if ( inputValid ) {
130 | int x = (number-1) % 3 ;
131 | int y = (number-1) / 3 ;
132 |
133 | if (!board[x][y].isEmpty()) {
134 | System.out.println("Field number " + number + "already taken");
135 | inputValid = false;
136 | } else {
137 | board[x][y].occupy(player);
138 | }
139 | }
140 | }
141 | }
142 |
143 | }
--------------------------------------------------------------------------------
/.idea/uiDesigner.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | -
6 |
7 |
8 | -
9 |
10 |
11 | -
12 |
13 |
14 | -
15 |
16 |
17 | -
18 |
19 |
20 |
21 |
22 |
23 | -
24 |
25 |
26 |
27 |
28 |
29 | -
30 |
31 |
32 |
33 |
34 |
35 | -
36 |
37 |
38 |
39 |
40 |
41 | -
42 |
43 |
44 |
45 |
46 | -
47 |
48 |
49 |
50 |
51 | -
52 |
53 |
54 |
55 |
56 | -
57 |
58 |
59 |
60 |
61 | -
62 |
63 |
64 |
65 |
66 | -
67 |
68 |
69 |
70 |
71 | -
72 |
73 |
74 | -
75 |
76 |
77 |
78 |
79 | -
80 |
81 |
82 |
83 |
84 | -
85 |
86 |
87 |
88 |
89 | -
90 |
91 |
92 |
93 |
94 | -
95 |
96 |
97 |
98 |
99 | -
100 |
101 |
102 | -
103 |
104 |
105 | -
106 |
107 |
108 | -
109 |
110 |
111 | -
112 |
113 |
114 |
115 |
116 | -
117 |
118 |
119 | -
120 |
121 |
122 |
123 |
124 |
--------------------------------------------------------------------------------