Created
June 18, 2016 16:27
-
-
Save alvareztech/6cc6b66946a6fb6a95678c2e808104e8 to your computer and use it in GitHub Desktop.
Workshop 1: Juego 3 en raya. Curso de desarrollo de aplicaciones Android. Android I
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="tech.alvarez.tresenraya.MainActivity"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal"> | |
<EditText | |
android:id="@+id/jugador1EditText" | |
android:layout_width="0dp" | |
android:layout_height="48dp" | |
android:layout_weight="1" | |
android:hint="@string/jugador1" /> | |
<EditText | |
android:id="@+id/jugador2EditText" | |
android:layout_width="0dp" | |
android:layout_height="48dp" | |
android:layout_weight="1" | |
android:hint="@string/jugador2" /> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="center_horizontal" | |
android:orientation="horizontal"> | |
<Button | |
android:id="@+id/unoButton" | |
android:layout_width="80dp" | |
android:layout_height="80dp" | |
android:onClick="presionar1" | |
android:textSize="50sp" /> | |
<Button | |
android:id="@+id/dosButton" | |
android:layout_width="80dp" | |
android:layout_height="80dp" | |
android:onClick="presionar2" | |
android:textSize="50sp" /> | |
<Button | |
android:id="@+id/tresButton" | |
android:layout_width="80dp" | |
android:layout_height="80dp" | |
android:onClick="presionar3" | |
android:textSize="50sp" /> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="center_horizontal" | |
android:orientation="horizontal"> | |
<Button | |
android:id="@+id/cuatroButton" | |
android:layout_width="80dp" | |
android:layout_height="80dp" | |
android:onClick="presionar4" | |
android:textSize="50sp" /> | |
<Button | |
android:id="@+id/cincoButton" | |
android:layout_width="80dp" | |
android:layout_height="80dp" | |
android:onClick="presionar5" | |
android:textSize="50sp" /> | |
<Button | |
android:id="@+id/seisButton" | |
android:layout_width="80dp" | |
android:layout_height="80dp" | |
android:onClick="presionar6" | |
android:textSize="50sp" /> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="center_horizontal" | |
android:orientation="horizontal"> | |
<Button | |
android:id="@+id/sieteButton" | |
android:layout_width="80dp" | |
android:layout_height="80dp" | |
android:onClick="presionar7" | |
android:textSize="50sp" /> | |
<Button | |
android:id="@+id/ochoButton" | |
android:layout_width="80dp" | |
android:layout_height="80dp" | |
android:onClick="presionar8" | |
android:textSize="50sp" /> | |
<Button | |
android:id="@+id/nueveButton" | |
android:layout_width="80dp" | |
android:layout_height="80dp" | |
android:onClick="presionar9" | |
android:textSize="50sp" /> | |
</LinearLayout> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/historialRecyclerView" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
android { | |
compileSdkVersion 23 | |
buildToolsVersion "23.0.3" | |
defaultConfig { | |
applicationId "tech.alvarez.tresenraya" | |
minSdkVersion 15 | |
targetSdkVersion 23 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
testCompile 'junit:junit:4.12' | |
compile 'com.android.support:appcompat-v7:23.4.0' | |
compile 'com.android.support:design:23.4.0' | |
compile 'com.android.support:cardview-v7:23.4.0' | |
compile 'com.android.support:recyclerview-v7:23.4.0' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<color name="colorPrimary">#00BCD4</color> | |
<color name="colorPrimaryDark">#0097A7</color> | |
<color name="colorAccent">#FFD740</color> | |
<color name="colorPrimaryText">#DE000000</color> | |
<color name="colorSecondaryText">#8A000000</color> | |
<color name="colorDisabledText">#61000000</color> | |
<color name="colorPrimaryDarkText">#FFFFFF</color> | |
<color name="colorSecondaryDarkText">#B3FFFFFF</color> | |
<color name="colorDisabledDarkText">#80FFFFFF</color> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="72dp" | |
android:orientation="horizontal" | |
android:paddingBottom="8dp" | |
android:paddingLeft="16dp" | |
android:paddingRight="16dp" | |
android:paddingTop="8dp"> | |
<ImageView | |
android:id="@+id/iconoImageView" | |
android:layout_width="24dp" | |
android:layout_height="24dp" | |
android:layout_gravity="center_vertical" | |
android:src="@mipmap/ic_launcher" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center_vertical" | |
android:orientation="vertical" | |
android:paddingLeft="32dp" | |
android:paddingRight="16dp"> | |
<TextView | |
android:id="@+id/tituloTextView" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/app_name" | |
android:textColor="@color/colorPrimaryText" | |
android:textSize="16sp" /> | |
<TextView | |
android:id="@+id/subtituloTextView" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/app_name" | |
android:textColor="@color/colorSecondaryText" | |
android:textSize="14sp" /> | |
</LinearLayout> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.tresenraya; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import java.util.Calendar; | |
import java.util.Date; | |
import tech.alvarez.tresenraya.adapters.PartidasAdapter; | |
import tech.alvarez.tresenraya.models.Partida; | |
public class MainActivity extends AppCompatActivity { | |
private EditText jugador1EdiText; | |
private EditText jugador2EdiText; | |
private Button unoButton; | |
private Button dosButton; | |
private Button tresButton; | |
private Button cuatroButton; | |
private Button cincoButton; | |
private Button seisButton; | |
private Button sieteButton; | |
private Button ochoButton; | |
private Button nueveButton; | |
private RecyclerView historialRecyclerView; | |
private PartidasAdapter partidasAdapter; | |
private boolean tocaX = true; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
jugador1EdiText = (EditText) findViewById(R.id.jugador1EditText); | |
jugador2EdiText = (EditText) findViewById(R.id.jugador2EditText); | |
unoButton = (Button) findViewById(R.id.unoButton); | |
dosButton = (Button) findViewById(R.id.dosButton); | |
tresButton = (Button) findViewById(R.id.tresButton); | |
cuatroButton = (Button) findViewById(R.id.cuatroButton); | |
cincoButton = (Button) findViewById(R.id.cincoButton); | |
seisButton = (Button) findViewById(R.id.seisButton); | |
sieteButton = (Button) findViewById(R.id.sieteButton); | |
ochoButton = (Button) findViewById(R.id.ochoButton); | |
nueveButton = (Button) findViewById(R.id.nueveButton); | |
historialRecyclerView = (RecyclerView) findViewById(R.id.historialRecyclerView); | |
historialRecyclerView.setHasFixedSize(true); | |
historialRecyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
partidasAdapter = new PartidasAdapter(); | |
historialRecyclerView.setAdapter(partidasAdapter); | |
} | |
public void presionar1(View view) { | |
if (unoButton.getText().toString().equals("")) { | |
if (tocaX) { | |
unoButton.setText("X"); | |
verificarSiGano("X"); | |
} else { | |
unoButton.setText("O"); | |
verificarSiGano("O"); | |
} | |
tocaX = !tocaX; | |
} | |
} | |
public void presionar2(View view) { | |
if (dosButton.getText().toString().equals("")) { | |
if (tocaX) { | |
dosButton.setText("X"); | |
verificarSiGano("X"); | |
} else { | |
dosButton.setText("O"); | |
verificarSiGano("O"); | |
} | |
tocaX = !tocaX; | |
} | |
} | |
public void presionar3(View view) { | |
if (tresButton.getText().toString().equals("")) { | |
if (tocaX) { | |
tresButton.setText("X"); | |
verificarSiGano("X"); | |
} else { | |
tresButton.setText("O"); | |
verificarSiGano("O"); | |
} | |
tocaX = !tocaX; | |
} | |
} | |
public void presionar4(View view) { | |
if (cuatroButton.getText().toString().equals("")) { | |
if (tocaX) { | |
cuatroButton.setText("X"); | |
verificarSiGano("X"); | |
} else { | |
cuatroButton.setText("O"); | |
verificarSiGano("O"); | |
} | |
tocaX = !tocaX; | |
} | |
} | |
public void presionar5(View view) { | |
if (cincoButton.getText().toString().equals("")) { | |
if (tocaX) { | |
cincoButton.setText("X"); | |
verificarSiGano("X"); | |
} else { | |
cincoButton.setText("O"); | |
verificarSiGano("O"); | |
} | |
tocaX = !tocaX; | |
} | |
} | |
public void presionar6(View view) { | |
if (seisButton.getText().toString().equals("")) { | |
if (tocaX) { | |
seisButton.setText("X"); | |
verificarSiGano("X"); | |
} else { | |
seisButton.setText("O"); | |
verificarSiGano("O"); | |
} | |
tocaX = !tocaX; | |
} | |
} | |
public void presionar7(View view) { | |
if (sieteButton.getText().toString().equals("")) { | |
if (tocaX) { | |
sieteButton.setText("X"); | |
verificarSiGano("X"); | |
} else { | |
sieteButton.setText("O"); | |
verificarSiGano("O"); | |
} | |
tocaX = !tocaX; | |
} | |
} | |
public void presionar8(View view) { | |
if (ochoButton.getText().toString().equals("")) { | |
if (tocaX) { | |
ochoButton.setText("X"); | |
verificarSiGano("X"); | |
} else { | |
ochoButton.setText("O"); | |
verificarSiGano("O"); | |
} | |
tocaX = !tocaX; | |
} | |
} | |
public void presionar9(View view) { | |
if (nueveButton.getText().toString().equals("")) { | |
if (tocaX) { | |
nueveButton.setText("X"); | |
verificarSiGano("X"); | |
} else { | |
nueveButton.setText("O"); | |
verificarSiGano("O"); | |
} | |
tocaX = !tocaX; | |
} | |
} | |
public boolean gano(String simbolo) { | |
boolean siHayGanador = false; | |
if (unoButton.getText().equals(simbolo) && dosButton.getText().equals(simbolo) && tresButton.getText().equals(simbolo)) { | |
siHayGanador = true; | |
} | |
if (unoButton.getText().equals(simbolo) && cuatroButton.getText().equals(simbolo) && sieteButton.getText().equals(simbolo)) { | |
siHayGanador = true; | |
} | |
if (unoButton.getText().equals(simbolo) && cincoButton.getText().equals(simbolo) && nueveButton.getText().equals(simbolo)) { | |
siHayGanador = true; | |
} | |
if (cuatroButton.getText().equals(simbolo) && cincoButton.getText().equals(simbolo) && seisButton.getText().equals(simbolo)) { | |
siHayGanador = true; | |
} | |
if (tresButton.getText().equals(simbolo) && seisButton.getText().equals(simbolo) && nueveButton.getText().equals(simbolo)) { | |
siHayGanador = true; | |
} | |
if (sieteButton.getText().equals(simbolo) && ochoButton.getText().equals(simbolo) && nueveButton.getText().equals(simbolo)) { | |
siHayGanador = true; | |
} | |
if (tresButton.getText().equals(simbolo) && cincoButton.getText().equals(simbolo) && sieteButton.getText().equals(simbolo)) { | |
siHayGanador = true; | |
} | |
if (dosButton.getText().equals(simbolo) && cincoButton.getText().equals(simbolo) && ochoButton.getText().equals(simbolo)) { | |
siHayGanador = true; | |
} | |
return siHayGanador; | |
} | |
public void verificarSiGano(String simbolo) { | |
if (gano(simbolo)) { | |
Toast.makeText(getApplicationContext(), "Gano " + simbolo + "!!!", Toast.LENGTH_SHORT).show(); | |
String nombreJugador1 = jugador1EdiText.getText().toString(); | |
String nombreJugador2 = jugador2EdiText.getText().toString(); | |
int quienGano = 0; | |
if (simbolo == "X") { | |
quienGano = 1; | |
} | |
if (simbolo == "O") { | |
quienGano = 2; | |
} | |
Date fechaActual = Calendar.getInstance().getTime(); | |
Partida partida = new Partida(nombreJugador1, nombreJugador2, quienGano, fechaActual); | |
partidasAdapter.add(partida); | |
limpiar(); | |
} | |
} | |
private void limpiar() { | |
unoButton.setText(""); | |
dosButton.setText(""); | |
tresButton.setText(""); | |
cuatroButton.setText(""); | |
cincoButton.setText(""); | |
seisButton.setText(""); | |
sieteButton.setText(""); | |
ochoButton.setText(""); | |
nueveButton.setText(""); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.tresenraya.models; | |
import java.util.Date; | |
/** | |
* Created by danyalvarez on 17/6/16. | |
*/ | |
public class Partida { | |
private String nombreJugador1; | |
private String nombreJugador2; | |
private int quienGano; | |
private Date fecha; | |
public Partida(String nombreJugador1, String nombreJugador2, int quienGano, Date fecha) { | |
this.nombreJugador1 = nombreJugador1; | |
this.nombreJugador2 = nombreJugador2; | |
this.quienGano = quienGano; | |
this.fecha = fecha; | |
} | |
public String getNombreJugador1() { | |
return nombreJugador1; | |
} | |
public void setNombreJugador1(String nombreJugador1) { | |
this.nombreJugador1 = nombreJugador1; | |
} | |
public String getNombreJugador2() { | |
return nombreJugador2; | |
} | |
public void setNombreJugador2(String nombreJugador2) { | |
this.nombreJugador2 = nombreJugador2; | |
} | |
public int getQuienGano() { | |
return quienGano; | |
} | |
public void setQuienGano(int quienGano) { | |
this.quienGano = quienGano; | |
} | |
public Date getFecha() { | |
return fecha; | |
} | |
public void setFecha(Date fecha) { | |
this.fecha = fecha; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tech.alvarez.tresenraya.adapters; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import tech.alvarez.tresenraya.R; | |
import tech.alvarez.tresenraya.models.Partida; | |
/** | |
* Created by danyalvarez on 17/6/16. | |
*/ | |
public class PartidasAdapter extends RecyclerView.Adapter<PartidasAdapter.ViewHolder> { | |
private ArrayList<Partida> dataset; | |
public PartidasAdapter() { | |
dataset = new ArrayList<>(); | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_partida, parent, false); | |
return new ViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder holder, int position) { | |
Partida partida = dataset.get(position); | |
if (partida.getQuienGano() == 1) { | |
holder.tituloTextView.setText(partida.getNombreJugador1()); | |
} else { | |
holder.tituloTextView.setText(partida.getNombreJugador2()); | |
} | |
holder.subtituloTextView.setText(darFormato(partida.getFecha())); | |
} | |
@Override | |
public int getItemCount() { | |
return dataset.size(); | |
} | |
public static String darFormato(Date date) { | |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ddMMMyyyy hh:mm"); | |
return simpleDateFormat.format(date).toUpperCase(); | |
} | |
public static class ViewHolder extends RecyclerView.ViewHolder { | |
TextView tituloTextView; | |
TextView subtituloTextView; | |
public ViewHolder(View itemView) { | |
super(itemView); | |
tituloTextView = (TextView) itemView.findViewById(R.id.tituloTextView); | |
subtituloTextView = (TextView) itemView.findViewById(R.id.subtituloTextView); | |
} | |
} | |
public void add(Partida partida) { | |
dataset.add(partida); | |
notifyDataSetChanged(); | |
} | |
public void clear() { | |
dataset.clear(); | |
notifyDataSetChanged(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<string name="app_name">Tres En Raya</string> | |
<string name="jugador1">Jugador 1</string> | |
<string name="jugador2">Jugador 2</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment