GithubHelp home page GithubHelp logo

android-course's People

Watchers

 avatar  avatar

android-course's Issues

problems in coding draw

package com.appladyce.elmundo.connectthree;

import android.content.pm.ActivityInfo;
import android.media.Image;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

//set player to 1 for red player and 2 for yellow player
int activePlayer = 1;
boolean gameActive = true;

TextView winMessage;

int[] gameState = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int [][] winPositions = {{0,1,2},{3,4,5},{6,7,8}, {0,3,6}, {1,4,7}, {2,5,8},{0,4,8}, {2,4,6}};

public void dropIn(View view) {
    ImageView tileTapped = (ImageView) view;

String counterTag = tileTapped.getTag().toString();
  int positionId =  Integer.parseInt(counterTag);

    if (gameState[positionId]==0 && gameActive)

        gameState[positionId] = activePlayer;
        tileTapped.setTranslationY(-1000f);

        if (activePlayer == 1) {
            tileTapped.setImageResource(R.drawable.red);


            activePlayer = 2;
        }

        else {

            tileTapped.setImageResource(R.drawable.yellow);


            activePlayer = 1;

        }

        tileTapped.animate().translationYBy(1000f).rotation(360).setDuration(300);

        String winner = "";
        for(int []winPosition :winPositions) {
            if (gameState[winPosition[0]] == gameState[winPosition[1]] && gameState[winPosition[1]] == gameState[winPosition[2]] && gameState[winPosition[0]] != 0)

            {
                if (gameState[winPosition[0]] == 1 && gameActive) {
                    winner = "Red";
                    gameActive = false;

                    LinearLayout layout = (LinearLayout) findViewById(R.id.innerLayout);
                    // layout.setTranslationX(-1000f);
                    layout.setVisibility(View.VISIBLE);

                    //layout.animate().translationX(1000f).setDuration(100);

                    winMessage = (TextView) findViewById(R.id.winning_message);

                    winMessage.setText(winner + " has  won!");
                }

                else if (gameState[winPosition[0]] == 2 && gameActive){

                    winner = "Yellow";
                    gameActive = false;
                    LinearLayout layout = (LinearLayout) findViewById(R.id.innerLayout);
                    // layout.setTranslationX(-1000f);
                    layout.setVisibility(View.VISIBLE);

                    //layout.animate().translationX(1000f).setDuration(100);

                    // winMessage = (TextView) findViewById(R.id.winning_message);

                    winMessage.setText(winner + " has  won!");


                }

// end

            }//end of if statement

//PROBLEM AREA IN CODE
else {
boolean gameIsOver = true;
for (int counterState : gameState) {

                    if (counterState == 0) {

                        gameIsOver = false;
                    }

                }

                if (gameIsOver) {
                    LinearLayout layout = (LinearLayout) findViewById(R.id.innerLayout);
                    layout.setVisibility(View.VISIBLE);
                    winMessage.setText(" It's a draw!");
                }
            }

        }




    }





public void playAgain(View view){

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

    }
    gameActive = true;
     activePlayer = 2;
    LinearLayout layout = (LinearLayout)findViewById(R.id.innerLayout);
    //layout.setTranslationX(-1000f);
    layout.setVisibility(View.INVISIBLE);

// set images to blak on a grid layout
GridLayout grid = (GridLayout)findViewById(R.id.gridLayout);
for (int i = 0; i<grid.getChildCount(); i++){
((ImageView) grid.getChildAt(i)).setImageResource(0);

    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.