-
Notifications
You must be signed in to change notification settings - Fork 0
/
VsPlayer.java
168 lines (162 loc) · 8.27 KB
/
VsPlayer.java
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.mobile.sampleapp;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by hazeezadebayo on 4/10/2018.
*/
public class VsPlayer extends projectFour {
int w[][];
int i, j, k = 0;
int z11=0,z12=0,z13=0,z21=0,z22=0,z23=0,z31=0,z32=0,z33=0;
Button feedback, p[][];
TextView textView;
//Called when the activity is first created
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add("New Game"); return true;}
public boolean onOptionsItemSelected(MenuItem item) {
Intent intents = new Intent(VsPlayer.this, VsPlayer.class);
intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intents);
setvsBoard();
return true; }
//Set up the game board.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tictactoe);
setvsBoard();
feedback = (Button) findViewById(R.id.feedBack);
feedback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent emailAppIntent = new Intent(Intent.ACTION_SENDTO);
emailAppIntent.setData(Uri.parse("mailto:"));
emailAppIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
emailAppIntent.putExtra(Intent.EXTRA_SUBJECT, "Feedback from App");
// emailAppIntent.putExtra(Intent.EXTRA_TEXT, feedbackk);
if (emailAppIntent.resolveActivity(getPackageManager()) != null) {
startActivity(emailAppIntent);
}
}
});
}
public void setvsBoard() {
k = 0;
p = new Button[4][4];
w = new int[4][4];
textView = (TextView) findViewById(R.id.dialogue);
p[1][3] = (Button) findViewById(R.id.one);
p[1][2] = (Button) findViewById(R.id.two);
p[1][1] = (Button) findViewById(R.id.three);
p[2][3] = (Button) findViewById(R.id.four);
p[2][2] = (Button) findViewById(R.id.five);
p[2][1] = (Button) findViewById(R.id.six);
p[3][3] = (Button) findViewById(R.id.seven);
p[3][2] = (Button) findViewById(R.id.eight);
p[3][1] = (Button) findViewById(R.id.nine);
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++)
w[i][j] = 2;
}
////////////////////////////////////////////////////////
textView.setText("Click a button to start.");
// add the click listeners for each button
p[1][3].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { if(z13==0) {
if (k==1){ k=0; p[1][3].setText("O"); w[1][3] = 0; textView.setText("player 1 turn"); checkBoardvs();}
else if (k==0) {k=1; p[1][3].setText("X"); w[1][3] = 1; textView.setText("player 2 turn"); checkBoardvs();} z13=1; } }
});
p[1][2].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { if(z12==0) {
if (k==1){ k=0; p[1][2].setText("O"); w[1][2] = 0; textView.setText("player 1 turn"); checkBoardvs();}
else if (k==0) {k=1; p[1][2].setText("X"); w[1][2] = 1; textView.setText("player 2 turn"); checkBoardvs();} z12=1;} }
});
p[1][1].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { if(z11==0) {
if (k==1){ k=0; p[1][1].setText("O"); w[1][1] = 0; textView.setText("player 1 turn"); checkBoardvs();}
else if (k==0) {k=1; p[1][1].setText("X"); w[1][1] = 1; textView.setText("player 2 turn"); checkBoardvs();} z11=1;} }
});
p[2][3].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { if(z23==0) {
if (k==1){ k=0; p[2][3].setText("O"); w[2][3] = 0; textView.setText("player 1 turn"); checkBoardvs();}
else if (k==0) {k=1; p[2][3].setText("X"); w[2][3] = 1; textView.setText("player 2 turn"); checkBoardvs();} z23=1;}}
});
p[2][2].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { if(z22==0) {
if (k==1){ k=0; p[2][2].setText("O"); w[2][2] = 0; textView.setText("player 1 turn"); checkBoardvs();}
else if (k==0) {k=1; p[2][2].setText("X"); w[2][2] = 1; textView.setText("player 2 turn"); checkBoardvs();} z22=1;}}
});
p[2][1].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { if(z21==0) {
if (k==1){ k=0; p[2][1].setText("O"); w[2][1] = 0; textView.setText("player 1 turn"); checkBoardvs();}
else if (k==0) {k=1; p[2][1].setText("X"); w[2][1] = 1; textView.setText("player 2 turn"); checkBoardvs();} z21=1;}}
});
p[3][3].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { if(z33==0) {
if (k==1){ k=0; p[3][3].setText("O"); w[3][3] = 0; textView.setText("player 1 turn"); checkBoardvs();}
else if (k==0) {k=1; p[3][3].setText("X"); w[3][3] = 1; textView.setText("player 2 turn"); checkBoardvs();} z33=1;}}
});
p[3][2].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { if(z32==0) {
if (k==1){ k=0; p[3][2].setText("O"); w[3][2] = 0; textView.setText("player 1 turn"); checkBoardvs();}
else if (k==0) {k=1; p[3][2].setText("X"); w[3][2] = 1; textView.setText("player 2 turn"); checkBoardvs();} z32=1;}}
});
p[3][1].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { if(z31==0) {
if (k==1){ k=0; p[3][1].setText("O"); w[3][1] = 0; textView.setText("player 1 turn"); checkBoardvs();}
else if (k==0) {k=1; p[3][1].setText("X"); w[3][1] = 1; textView.setText("player 2 turn"); checkBoardvs();} z31=1;}}
});
}
public void checkBoardvs() {
if ((w[1][1] == 0 && w[2][2] == 0 && w[3][3] == 0)
|| (w[1][3] == 0 && w[2][2] == 0 && w[3][1] == 0)
|| (w[1][2] == 0 && w[2][2] == 0 && w[3][2] == 0)
|| (w[1][3] == 0 && w[2][3] == 0 && w[3][3] == 0)
|| (w[1][1] == 0 && w[1][2] == 0 && w[1][3] == 0)
|| (w[2][1] == 0 && w[2][2] == 0 && w[2][3] == 0)
|| (w[3][1] == 0 && w[3][2] == 0 && w[3][3] == 0)
|| (w[1][1] == 0 && w[2][1] == 0 && w[3][1] == 0))
{textView.setText("player 1 wins!");
} else if ((w[1][1] == 1 && w[2][2] == 1 && w[3][3] == 1)
|| (w[1][3] == 1 && w[2][2] == 1 && w[3][1] == 1)
|| (w[1][2] == 1 && w[2][2] == 1 && w[3][2] == 1)
|| (w[1][3] == 1 && w[2][3] == 1 && w[3][3] == 1)
|| (w[1][1] == 1 && w[1][2] == 1 && w[1][3] == 1)
|| (w[2][1] == 1 && w[2][2] == 1 && w[2][3] == 1)
|| (w[3][1] == 1 && w[3][2] == 1 && w[3][3] == 1)
|| (w[1][1] == 1 && w[2][1] == 1 && w[3][1] == 1))
{textView.setText("player 2 wins!");}
}
@Override
public void onBackPressed() {
super.onBackPressed();
try {
Intent intents = new Intent(VsPlayer.this, TicTacToe.class);
intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intents);
finish();
} catch (Exception e) {
e.printStackTrace();}
}
}