You are on page 1of 10

C:/Android/chinwe/NetBeansProjects/ThreeInARowGame/src/threeInARowGame/ThreeInARowGame.

java
/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package threeInARowGame;
import java.util.Scanner;
/**
*

* @author Benjamin Chinwe Detta ett spelprogram fr tv spelare. Den frsta att
* f sin tagg i tre platser i rad vinner spelet.
*/

public final class ThreeInARowGame {


private String[][] table; // spelbord matris
String playerLebel;

// klass konstruktr

public ThreeInARowGame() {

table = new String[3][3]; // reservera minnesutrymme fr spelbord

Scanner inputLetter = new Scanner(System.in); // read from standard in


int moves = 0;

System.out.println("Detta ett spelprogram fr tv spelare.\n"

+ "Den frsta att f sin tagg i tre platser i rad vinner spelet.");

System.out.println();

System.out.println("Lt oss spela tre-i-rad (ThreeInARowGame). ");

System.out.print("Vi har tv spelare X och O. Vlj som spelar frsta X eller O !: "

playerLebel = inputLetter.nextLine().toUpperCase(); //Acceptera spelare tag och kon


createBoard(); // kalla metod fr att visa spelplanen.

// while statement - att iterera att acceptera koordinaterna och kontrollera vinnar
while (moves <= 9) {

if (moves > 8) { // if-statement att kontrollera om spela slut att rita


createBoard();

System.out.println("A Draw!");

1.1 of 10

break;

2015.09.24 01:17:03

C:/Android/chinwe/NetBeansProjects/ThreeInARowGame/src/threeInARowGame/ThreeInARowGame.java

t Properties.

Den frsta att

vinner spelet.");

lar frsta X eller O !: ");

ptera spelare tag och konvertera till vre brev

na och kontrollera vinnare eller rita

ela slut att rita

1.2 of 10

2015.09.24 01:17:03

C:/Android/chinwe/NetBeansProjects/ThreeInARowGame/src/threeInARowGame/ThreeInARowGame.java

if (playerLebel.equals("X")) { //if-statement att kontrollera om spelare tag r

makeMove(inputLetter, playerLebel); // metod fr att ta emot spelare X spel


if (moves > 4) {

createBoard(); // kalla metod fr att visa spelplanen.

if (checkWinner(playerLebel)) { // if-statement att kontrollera om spel


System.out.println(playerLebel + " You Win!!!");

break;

} else {

makeMove(inputLetter, playerLebel); //metod fr att ta emot spelare O spel


if (moves > 4) {

if (checkWinner(playerLebel)) { //if-statement att kontrollera om spela


createBoard(); // kalla metod fr att visa spelplanen.
System.out.println(playerLebel + " You Win!!!");

break;

createBoard(); // kalla metod fr att visa spelplanen.

moves++; // rknare fr att hlla rkningen p antalet drag under spelets gng
if (playerLebel.equals("X")) {
playerLebel = "O";

} else {
}

playerLebel = "X";

/**

* @param args the command line arguments

* main metoden fr att starta programmet.


*/

public static void main(String[] args) {


// TODO code application logic here

}
2.1 of 10

ThreeInARowGame threeInARowGame = new ThreeInARowGame();

2015.09.24 01:17:03

C:/Android/chinwe/NetBeansProjects/ThreeInARowGame/src/threeInARowGame/ThreeInARowGame.java

rollera om spelare tag r X eller O

tt ta emot spelare X spel koordinater

t att kontrollera om spelare X r vinner

t ta emot spelare O spel koordinater


att kontrollera om spelare O r vinner

drag under spelets gng

2.2 of 10

2015.09.24 01:17:03

C:/Android/chinwe/NetBeansProjects/ThreeInARowGame/src/threeInARowGame/ThreeInARowGame.java
// metod fr att visa spelplanen.
public void createBoard() {
System.out.println();

for (String[] table1 : table) {

for (int j = 0; j < table1.length; j++) {


if (table1[j] == null) {

System.out.print("_");

} else {
}

System.out.print(table1[j]);

if (j < 2) {

System.out.print("|");

} else {

System.out.println();

public void makeMove(Scanner enteredLetter, String playerAlphabet) {


int r;
int c;

Boolean goodInput = false;


while (!goodInput) {
r = -1;
c = -1;

System.out.println("Player " + playerAlphabet + " enter coordinates to play ");


System.out.println(playerAlphabet + ": 0 - 2) : ");

if (enteredLetter.hasNextInt()) { // must be integers


}

r = enteredLetter.nextInt();

System.out.println(playerAlphabet + ": 0 - 2) : ");


if (enteredLetter.hasNextInt()) {
c = enteredLetter.nextInt();

} else {

enteredLetter.nextLine(); // consume a line without an integer

System.out.println("Both inputs must be integers between 0 and 2.");


}

continue;

// must be in the right coordinate range


3.1 of 10

2015.09.24 01:17:03

C:/Android/chinwe/NetBeansProjects/ThreeInARowGame/src/threeInARowGame/ThreeInARowGame.java

r coordinates to play ");

etween 0 and 2.");

3.2 of 10

2015.09.24 01:17:03

C:/Android/chinwe/NetBeansProjects/ThreeInARowGame/src/threeInARowGame/ThreeInARowGame.java
if ((r < 0) || (r > 2) || (c < 0) || (c > 2)) {

System.out.println("Both inputs must be integers between 0 and 2.");

} // make sure the space is not occupied


else if (table[r][c] != null) {

System.out.println("That location is occupied");

} else {

table[r][c] = playerAlphabet;

return;

public boolean checkWinner(String playerAlphabet) {


int playInRow = 0;

int playDiagonal1 = 0;
int playDiagonal2 = 0;

int[] playInColumn = new int[table[0].length]; // assumes square table


for (int i = 0; i < table.length; i++) {
playInRow = 0;

for (int j = 0; j < table[i].length; j++) {


if (table[i][j] == null) {
}

continue;

if (table[i][j].equals(playerAlphabet)) {
playInRow++;

playInColumn[j]++;
if (i == j) {

playDiagonal1++;

} else if (i + j == 2) {

playDiagonal2++;

if (playInRow == 3) {
}

return true;

if (playDiagonal1 == 3 || playDiagonal2 == 3) {
}
4.1 of 10

return true;

for (int i = 0; i < playInColumn.length; i++) {

2015.09.24 01:17:03

C:/Android/chinwe/NetBeansProjects/ThreeInARowGame/src/threeInARowGame/ThreeInARowGame.java

etween 0 and 2.");

4.2 of 10

2015.09.24 01:17:03

C:/Android/chinwe/NetBeansProjects/ThreeInARowGame/src/threeInARowGame/ThreeInARowGame.java
if (playInColumn[i] == 3) {
}
}

5.1 of 10

return true;

return false;

2015.09.24 01:17:03

C:/Android/chinwe/NetBeansProjects/ThreeInARowGame/src/threeInARowGame/ThreeInARowGame.java

5.2 of 10

2015.09.24 01:17:03

You might also like