Didine
Didine

Reputation: 17

How to improve my Tic Tac Toe game design?

i create a text based tic tac toe game in java ,i tried applying some OOP principals but things get complicated.

What to comment and how to comment properly? is it good idea to create two player object?

How to get rid of so many if else statements?

here is my TicTacToeGame.class :

import java.util.*;
import java.util.zip.*;
import java.nio.channels.*;
import org.apache.http.conn.scheme.*;

public class TicTacToeGame implements IGame
{
    private  Scanner in;

    static  Player currentPlayer;

    private static String PlayerInput;

    private String xMark="x";

    private String oMark="o";

    public static boolean isGameRunning=true;

    private  static Map<String,String>grid;

    Player playerOne;

    Player playerTwo;

    private  final String

    a1="a1",    a2="a2",    a3="a3",

    b1="b1",    b2="b2",    b3="b3",

    c1="c1",    c2="c2",    c3="c3";

//Switch player
    public void nextTurn()
    {
        // TODO: Implement this method



        if (currentPlayer.getMark() == playerOne.getMark())
        {


            currentPlayer = playerTwo;

        }
        else
        {

            currentPlayer = playerOne;

        }


    }

    @Override
    public  String getUserInput()
    {

        System.out.println("");
        System.out.print("enter cell:");
        return PlayerInput = in.nextLine();
    }

    //update cell state by updating grid hashmap 
    //
    @Override
    public void updateGame()
    {

        System.out.println("");
        System.out.println("");
        System.out.println("      1   2   3");
        System.out.println("   A) " + grid.get(a1) + " | " + grid.get(a2) + " | " + grid.get(a3) + "");
        System.out.println("     ---|---|---");
        System.out.println("   B) " + grid.get(b1) + " | " + grid.get(b2) + " | " + grid.get(b3) + "");
        System.out.println("     ---|---|---");
        System.out.println("   C) " + grid.get(c1) + " | " + grid.get(c2) + " | " + grid.get(c3) + "");


    }


    @Override

    //check userinput and game state 

    public  void updateState()
    {

        getUserInput();

        if (isValideInput() == false){


            updateState();

            return;

        }

        grid.put(PlayerInput, currentPlayer.getMark());

    if(isGridFull()&&!isAWin()){

        System.out.println("its a tie");

        updateGame();

        displayGameOver();

        isGameRunning=false;

    }else if(isAWin()){

        System.out.println(currentPlayer.getName()+" wins");

        updateGame();

        displayGameOver();

        isGameRunning=false;
    }

        else{

            System.out.println(currentPlayer.getName()+" ends his turn");
        nextTurn();

        }

    }

    @Override
    public void initLogic()
    {
        // TODO: Implement this method
        in = new Scanner(System.in);

        grid = new HashMap<String,String>();


        grid.put(a1, " "); grid.put(a2, " "); grid.put(a3, " "); 
        grid.put(b1, " "); grid.put(b2, " "); grid.put(b3, " ");
        grid.put(c1, " "); grid.put(c2, " "); grid.put(c3, " ");


        playerOne = new Player(); 
        playerTwo = new Player();

        playerOne.setMark(xMark);
        playerTwo.setMark(oMark);

        if (currentPlayer == null)
        {

            currentPlayer = playerOne;
        }
    }


    @ Override
    public boolean isValideInput()
    {

        if (grid.containsKey(PlayerInput))
        {

            return true;

        }

        System.out.println("invalid input");

        return false;
    }

    @Override

    public  boolean isAWin()
    {

        if ((grid.get(a1) + grid.get(a2) + grid.get(a3)).equals("ooo") ||
            (grid.get(b1) + grid.get(b2) + grid.get(b3)).equals("ooo") ||
            (grid.get(c1) + grid.get(c2) + grid.get(c3)).equals("ooo") ||
            (grid.get(a1) + grid.get(b1) + grid.get(c1)).equals("ooo") ||
            (grid.get(a2) + grid.get(b2) + grid.get(c2)).equals("ooo") ||
            (grid.get(a3) + grid.get(b3) + grid.get(c3)).equals("ooo") ||
            (grid.get(a1) + grid.get(b2) + grid.get(c3)).equals("ooo") ||
            (grid.get(a3) + grid.get(b2) + grid.get(c1)).equals("ooo") ||
            (grid.get(a1) + grid.get(a2) + grid.get(a3)).equals("xxx") ||
            (grid.get(b1) + grid.get(b2) + grid.get(b3)).equals("xxx") ||
            (grid.get(c1) + grid.get(c2) + grid.get(c3)).equals("xxx") ||
            (grid.get(a1) + grid.get(b1) + grid.get(c1)).equals("xxx") ||
            (grid.get(a2) + grid.get(b2) + grid.get(c2)).equals("xxx") ||
            (grid.get(a3) + grid.get(b3) + grid.get(c3)).equals("xxx") ||
            (grid.get(a1) + grid.get(b2) + grid.get(c3)).equals("xxx") ||
            (grid.get(a3) + grid.get(b2) + grid.get(c1)).equals("xxx"))
        {

            return true;



        }

        return false;
    }


    public boolean isATie(){

        if(isGridFull()&&(!isAWin())){



            return true;
        }

        return false;
    }

    public boolean isGridFull(){

        if(!grid.containsValue(" ")){


            return true;
        }

        return false;
    }


    @Override
    public  void displayGameOver()
    {

        System.out.println("");
        System.out.println("            ===============");
        System.out.println("            ||           ||");
        System.out.println("            || game over ||");
        System.out.println("            ||           ||");
        System.out.println("            ===============");
    }


    @Override
    public boolean isGameRunning()
    {
        // TODO: Implement this method
        return isGameRunning;
    }

}

This is my Main.class:

import java.io.*;
import java.net.*;
import java.util.*;

public class Main
{



    public static void main(String[] args) {


        IGame game=new TicTacToeGame();

        game.initLogic();

main loop

        while(game.isGameRunning()==true){

            game.updateGame();

            game.updateState();

            }


        }

    }

any advices?

Upvotes: 0

Views: 437

Answers (4)

private static
private static

Reputation: 770

Apparently your xml file is not properly created. Try the following xml file it will definitely work

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout

 xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:fabview="http://schemas.android.com/apk/res-auto"
  android:id="@+id/main_coordinator_layout"
 android:layout_width="match_parent"
  android:layout_height="match_parent"
 android:fitsSystemWindows="true" >

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent" />

</android.support.design.widget.AppBarLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:orientation="vertical" >

 <!-- your stuff here -->

</LinearLayout>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ff00ff"
    android:src="@drawable/your_png_image"
    android:tint="#ffffff"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="35dp"
    android:layout_marginEnd="15dp" />
</android.support.design.widget.CoordinatorLayout>

I tried your own codes in my AIDE, and it force closed on startup. So i recreated the main.xml file with your exact other codes and it worked. And don't forget to import your package's R

Upvotes: 1

إياد الفواز
إياد الفواز

Reputation: 29

minSdkVersion 14

and at values folder in styles.xml

<style name="ActivityTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">#d55d55</item>
</style>

and in AndroidManifest.xml in activity tag:

android:theme="@style/ActivityTheme"

Upvotes: 1

Didine
Didine

Reputation: 17

i thought that there is a link between aide-ide m2repository and the problem that i had so i tried with android studio m2repository and it works, now my project compile like a charm and my app starts without any issues. I hope that will help somebody else.

Upvotes: 0

Ali Khaki
Ali Khaki

Reputation: 1212

in buil.gradle change

dependencies {
    compile 'com.android.support:design:25.+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

to

dependencies {
        compile 'com.android.support:design:21.+'
        compile fileTree(dir: 'libs', include: ['*.jar'])
    }

it is because you are using

compileSdkVersion 21

and

targetSdkVersion 21

if you want to use design support library version 25 you should use compileSdkVersio 25 and targetSdkVersion 25

and i recommend you to use dependencies like this

dependencies {
    compile 'com.android.support:design:25.0.0'

}

because when you using 'com.android.support:design:25.+' every time you sync gradle it want to check the last version in version 25.+ and its cause wasting time in sync .

Upvotes: 1

Related Questions