Tie Tac Toc was the first Android app I developed in my capstone class. In the XML code I used Nine buttons in the layout and made them all same width and height. When app loads all buttons are empty until pressed then changes either a X or an O. I use a function to check if there is a winner after user make his or her move. if the program has a winner it will throw out a toast message saying the winner is X or winner is O. If program has a draw it will also, throw out a toast for a tie. The XML code is showed on the left and java to make a successful functioning game on the right.
1: <?xml version="1.0" encoding="utf-8"?>
2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3: android:layout_width="fill_parent"
4: android:layout_height="fill_parent"
5: android:orientation="vertical"
6: android:id="@+id/root">
7: <TextView
8: android:layout_width="fill_parent"
9: android:layout_height="wrap_content"
10: android:text="@string/hello" />
11: <LinearLayout
12: android:layout_width="fill_parent"
13: android:layout_height="wrap_content"
14: android:orientation="horizontal"
15: android:paddingTop="40dp">
16: <Button
17: android:text="@string/One"
18: android:textColor="@color/white"
19: android:layout_width="fill_parent"
20: android:layout_height="fill_parent"
21: android:layout_weight="1"
22: android:paddingTop="40dp"
23: android:paddingBottom="40dp"
24: android:id="@+id/btnOne"
25: android:textSize="30dp"/>
26: <Button
27: android:text="@string/two"
28: android:paddingTop="40dp"
29: android:paddingBottom="40dp"
30: android:textColor="@color/white"
31: android:layout_width="fill_parent"
32: android:layout_height="fill_parent"
33: android:layout_weight="1"
34: android:id="@+id/btnTwo"
35: android:textSize="30dp"/>
36: <Button
37: android:text="@string/three"
38: android:paddingTop="40dp"
39: android:paddingBottom="40dp"
40: android:textColor="@color/white"
41: android:layout_width="fill_parent"
42: android:layout_height="fill_parent"
43: android:layout_weight="1"
44: android:id="@+id/btnThree"
45: android:textSize="30dp"/>
46: </LinearLayout>
47: <LinearLayout
48: android:orientation="horizontal"
49: android:layout_width="fill_parent"
50: android:layout_height="wrap_content">
51: <Button
52: android:text="@string/four"
53: android:paddingTop="40dp"
54: android:paddingBottom="40dp"
55: android:textColor="@color/white"
56: android:layout_width="fill_parent"
57: android:layout_height="fill_parent"
58: android:layout_weight="1"
59: android:id="@+id/btnFour"
60: android:textSize="30dp"/>
61: <Button
62: android:text="@string/five"
63: android:paddingTop="40dp"
64: android:paddingBottom="40dp"
65: android:textColor="@color/white"
66: android:layout_width="fill_parent"
67: android:layout_height="fill_parent"
68: android:layout_weight="1"
69: android:id="@+id/btnFive"
70: android:textSize="30dp"/>
71: <Button
72: android:text="@string/six"
73: android:paddingTop="40dp"
74: android:paddingBottom="40dp"
75: android:textColor="@color/white"
76: android:layout_width="fill_parent"
77: android:layout_height="fill_parent"
78: android:layout_weight="1"
79: android:id="@+id/btnSix"
80: android:textSize="30dp"/>
81: </LinearLayout>
82: <LinearLayout
83: android:orientation="horizontal"
84: android:layout_width="fill_parent"
85: android:layout_height="wrap_content">
86: <Button
87: android:text="@string/seven"
88: android:paddingTop="40dp"
89: android:paddingBottom="40dp"
90: android:textColor="@color/white"
91: android:layout_width="fill_parent"
92: android:layout_height="fill_parent"
93: android:layout_weight="1"
94: android:id="@+id/btnSeven"
95: android:textSize="30dp"/>
96: <Button
97: android:text="@string/eight"
98: android:paddingTop="40dp"
99: android:paddingBottom="40dp"
100: android:textColor="@color/white"
101: android:layout_width="fill_parent"
102: android:layout_height="fill_parent"
103: android:layout_weight="1"
104: android:id="@+id/btnEight"
105: android:textSize="30dp"/>
106: <Button
107: android:text="@string/nine"
108: android:paddingTop="40dp"
109: android:paddingBottom="40dp"
110: android:textColor="@color/white"
111: android:layout_width="fill_parent"
112: android:layout_height="fill_parent"
113: android:layout_weight="1"
114: android:id="@+id/btnNine"
115: android:textSize="30dp"/>
116: </LinearLayout>
117:</LinearLayout>
1. package tictactoe.layout;
2. import java.util.ArrayList;
3. import android.app.Activity;
4. import android.graphics.Color;
5. import android.os.Bundle;
6. import android.widget.Button;
7. import android.widget.TextView;
8. import android.widget.Toast;
9. import android.view.*;
10. public class TictactoeAppActivity extends Activity {
11. /** Called when the activity is first created. */
12. private ArrayList<Button> btn = new ArrayList<Button>();
13. public char mturn;
14. int flag = 0;
15. int count = 0;
16. int Xwins = 0;
17. int Owins = 0;
18. int Draw = 0;
19. @Override
20. public void onCreate(Bundle savedInstanceState) {
21. super.onCreate(savedInstanceState);
22. setContentView(R.layout.main);
23. init();
24. }
25. private void init(){
26. if (flag==1)
27. {
28. try {Thread.sleep(200);
29. } catch (InterruptedException e) {
30. // TODO Auto-generated catch block
31. e.printStackTrace();
32. }
33. }
34. btn.add((Button) findViewById(R.id.btnOne));
35. btn.add((Button) findViewById(R.id.btnTwo));
36. btn.add((Button) findViewById(R.id.btnThree));
37. btn.add((Button) findViewById(R.id.btnFour));
38. btn.add((Button) findViewById(R.id.btnFive));
39. btn.add((Button) findViewById(R.id.btnSix));
40. btn.add((Button) findViewById(R.id.btnSeven));
41. btn.add((Button) findViewById(R.id.btnEight));
42. btn.add((Button) findViewById(R.id.btnNine));
43. clearButtonsText();
44. mturn='x';
45. flag = 1;
46. count = 0;
47. addOnClick();
48. }
49. private void clearButtonsText(){
50. for(Button b : btn){
51. b.setText("");
52. b.setEnabled(true);
53. }
54. }
55. private void addOnClick() {
56. for(final Button bt : btn){
57. bt.setOnClickListener(new View.OnClickListener() {
58. public void onClick(View v) {
59. switch (mturn){
60. case 'x': bt.setText("X");
61. bt.setTextColor(Color.CYAN);
62. bt.setEnabled(false);
63. mturn = 'o';
64. checkXWinner();
65. break;
66. case 'o': bt.setText("O");
67. bt.setEnabled(false);
68. bt.setTextColor(Color.WHITE);
69. mturn = 'x';
70. checkOWinner();
71. break;
72. default : bt.setText("ERROR");
73. break;
74. }
75. }
76. });
77. }
78. }
79. public void checkXWinner()
80.{
81. ++count;
82. if(btn.get(0).getText()=="X" && btn.get(1).getText() == "X" && btn.get(2).getText()=="X")
83. {
84. ++Xwins;
85. Toast.makeText(getApplicationContext(), "X Wins!!!" + '\n' + "X won=" + Xwins, Toast.LENGTH_LONG).show();
86. init();
87. } else if (btn.get(3).getText()=="X" && btn.get(4).getText()=="X" && btn.get(5).getText()=="X"){
88. ++Xwins;
89. Toast.makeText(getApplicationContext(), "X Wins!!!" + '\n' + "X won=" + Xwins, Toast.LENGTH_LONG).show();
90. init();
91. } else if(btn.get(6).getText()=="X" && btn.get(7).getText()=="X" && btn.get(8).getText()=="X"){
92. ++Xwins;
93. Toast.makeText(getApplicationContext(), "X Wins!!!" + '\n' + "X won=" + Xwins, Toast.LENGTH_LONG).show();
94. init();
95. } else if(btn.get(0).getText()=="X" && btn.get(3).getText()=="X" && btn.get(6).getText()=="X"){
96. ++Xwins;
97. Toast.makeText(getApplicationContext(), "X Wins!!!" + '\n' + "X won=" + Xwins, Toast.LENGTH_LONG).show();
98. init();
99. } else if(btn.get(1).getText()=="X" && btn.get(4).getText()=="X" && btn.get(7).getText()=="X"){
100. ++Xwins;
101. Toast.makeText(getApplicationContext(), "X Wins!!!" + '\n' + "X won=" + Xwins, Toast.LENGTH_LONG).show();
102. init();
103. } else if(btn.get(2).getText()=="X" && btn.get(5).getText()=="X" && btn.get(8).getText()=="X"){
104. ++Xwins;
105. Toast.makeText(getApplicationContext(), "X Wins!!!" + '\n' + "X won=" + Xwins, Toast.LENGTH_LONG).show();
106. init();
107. } else if(btn.get(0).getText()=="X" && btn.get(4).getText()=="X" && btn.get(8).getText()=="X"){
108. ++Xwins;
109. Toast.makeText(getApplicationContext(), "X Wins!!!" + '\n' + "X won=" + Xwins, Toast.LENGTH_LONG).show();
110. init();
111. } else if(btn.get(2).getText()=="X" && btn.get(4).getText()=="X" && btn.get(6).getText()=="X"){
112. ++Xwins;
113. Toast.makeText(getApplicationContext(), "X Wins!!!" + '\n' + "X won=" + Xwins, Toast.LENGTH_LONG).show();
114. init();
115. } else if (count == 9)
116. {
117. ++Draw;
118. Toast.makeText(getApplicationContext(), "Draw!!!" + '\n'+ "Draw Count = " + Draw, Toast.LENGTH_LONG).show();
119. init();
120. }
121. }
122. public void checkOWinner()
123. {
124. ++count;
125. if(btn.get(0).getText()=="O" && btn.get(1).getText() == "O" && btn.get(2).getText()=="O")
126. {
127. ++Owins;
128. Toast.makeText(getApplicationContext(), "O Wins!!!" + '\n' + "O won=" + Owins, Toast.LENGTH_LONG).show();
129. init();
130. } else if (btn.get(3).getText()=="O" && btn.get(4).getText()=="O" && btn.get(5).getText()=="O"){
131. ++Owins;
132. Toast.makeText(getApplicationContext(), "O Wins!!!" + '\n' + "O won=" + Owins, Toast.LENGTH_LONG).show();
133. init();
134. } else if(btn.get(6).getText()=="O" && btn.get(7).getText()=="O" && btn.get(8).getText()=="O"){
135. ++Owins;
136. Toast.makeText(getApplicationContext(), "O Wins!!!" + '\n' + "O won=" + Owins, Toast.LENGTH_LONG).show();
137. init();
138. } else if(btn.get(0).getText()=="O" && btn.get(3).getText()=="O" && btn.get(6).getText()=="O"){
139. ++Owins;
140. Toast.makeText(getApplicationContext(), "O Wins!!!" + '\n' + "O won=" + Owins, Toast.LENGTH_LONG).show();
141. init();
142. } else if(btn.get(1).getText()=="O" && btn.get(4).getText()=="O" && btn.get(7).getText()=="O"){
143. ++Owins;
144. Toast.makeText(getApplicationContext(), "O Wins!!!" + '\n' + "O won=" + Owins, Toast.LENGTH_LONG).show();
145. init();
146. } else if(btn.get(2).getText()=="O" && btn.get(5).getText()=="O" && btn.get(8).getText()=="O"){
147. ++Owins;
148. Toast.makeText(getApplicationContext(), "O Wins!!!" + '\n' + "O won=" + Owins, Toast.LENGTH_LONG).show();
149. init();
150. } else if(btn.get(0).getText()=="O" && btn.get(4).getText()=="O" && btn.get(8).getText()=="O"){
151. ++Owins;
152. Toast.makeText(getApplicationContext(), "O Wins!!!" + '\n' + "O won=" + Owins, Toast.LENGTH_LONG).show();
153. init();
154. } else if(btn.get(2).getText()=="O" && btn.get(4).getText()=="O" && btn.get(6).getText()=="O"){
155. ++Owins;
156. Toast.makeText(getApplicationContext(), "O Wins!!!" + '\n' + "O won=" + Owins, Toast.LENGTH_LONG).show();
157. init();
158. } else if (count == 9)
159. {
160. ++Draw;
161. Toast.makeText(getApplicationContext(), "Draw!!!" + '\n'+ "Draw Count = " + Draw, Toast.LENGTH_LONG).show();
162. init();
163. }
164. }
165.}
The Gotcha time checker app is for Aiken Technical College students who can check their times for lab hours for all classes during the week of current semester. XML code on the left uses a input box and a button to submit student id number. The java code sends the submitted student id number to another java class which sends it to a php server that accesses the gotcha database with the correct username and password.
1: <?xml version="1.0" encoding="utf-8"?>
2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3: android:layout_width="wrap_content"
4: android:layout_height="match_parent"
5: android:orientation="vertical" >
6: <TextView
7: android:layout_width="fill_parent"
8: android:layout_height="0dp"
9: android:layout_weight="0.25"
10: android:background="@drawable/banner"/>
11: <TableLayout
12: android:id="@+id/tableLayout1"
13: android:layout_width="match_parent"
14: android:layout_height="0dp"
15: android:layout_weight="1" >
16: <TableRow
17: android:id="@+id/tableRow1"
18: android:layout_width="wrap_content"
19: android:layout_height="0dp"
20: android:layout_weight=".01" >
21: <TextView
22: android:id="@+id/Student_ID_Number"
23: android:layout_width="wrap_content"
24: android:layout_height="wrap_content"
25: android:layout_gravity="bottom"
26: android:text="Student ID Number: "
27: android:textAppearance="?android:attr/textAppearanceMedium" />
28: </TableRow>
29: <TableRow
30: android:id="@+id/tableRow2"
31: android:layout_width="wrap_content"
32: android:layout_height="0dp"
33: android:layout_weight=".001" >
34: <EditText
35: android:id="@+id/Student_ID_Number_insert"
36: android:layout_width="wrap_content"
37: android:layout_height="wrap_content"
38: android:layout_gravity="bottom"
39: android:layout_weight="1"
40: android:inputType="number" >
41: <requestFocus />
42: </EditText>
43: </TableRow>
44: <TableRow
45: android:id="@+id/tableRow4"
46: android:layout_width="wrap_content"
47: android:layout_height="wrap_content" >
48: <TextView
49: android:id="@+id/studiderror"
50: android:layout_width="wrap_content"
51: android:layout_height="wrap_content"
52: android:textAppearance="?android:attr/textAppearanceMedium" />
53: </TableRow>
54: <TableRow
55: android:id="@+id/tableRow3"
56: android:layout_width="wrap_content"
57: android:layout_height="0dp"
58: android:layout_weight=".01" >
59: <Button
60: android:id="@+id/button1"
61: android:layout_width="wrap_content"
62: android:layout_height="wrap_content"
63: android:layout_gravity="center_vertical"
64: android:layout_weight="1"
65: android:onClick="send"
66: android:text="Submit" />
67: </TableRow>
68: </TableLayout>
69:</LinearLayout>
1.package com.test;
2.import android.content.Intent;
3.import android.app.Activity;
4.import android.os.Bundle;
5.import android.view.View;
6.import android.view.View.OnClickListener;
7.import android.widget.Button;
8.import android.widget.EditText;
9.import android.widget.TextView;
10.public class test extends Activity implements OnClickListener {
11. /** Called when the activity is first created. */
12. @Override
13. public void onCreate(Bundle savedInstanceState) {
14. super.onCreate(savedInstanceState);
15. setContentView(R.layout.input);
16. Button submit = (Button) findViewById(R.id.button1);
17. submit.setOnClickListener(this);
18. }
19. @Override
20. public void onClick(View v) {
21. // TODO Auto-generated method stub
22. int flag = 0;
23. switch (v.getId()) {
24. case R.id.button1:
25. EditText studidinsert = (EditText) findViewById(R.id.Student_ID_Number_insert);
26. String mystudentid = studidinsert.getText().toString();
27. TextView myerror = (TextView) findViewById(R.id.studiderror);
28. if (mystudentid.length() != 7) {
29. flag = 1;
30. }
31. switch (flag) {
32. case 0:
33. myerror.setText("");
34. Intent mainMenuIntent = new Intent(v.getContext(),getdata.class);
35. mainMenuIntent.putExtra("studentident", mystudentid);
36. v.getContext().startActivity(mainMenuIntent);
37. break;
38. case 1:
39. myerror.setText("Please enter a valid student id number.");
40. break;
41. }
42. break;
43. }
44. }
45.}
The image above shows you student name, id number and number of classes which only have one class recorded for the week. If student has not signed out it will show that student is still currently logged into that class.
1.<?xml version="1.0" encoding="utf-8"?>
2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:id="@+id/myLinearLayout"
4. android:layout_width="fill_parent"
5. android:layout_height="fill_parent"
6. android:orientation="vertical" >
7. <TextView
8. android:layout_width="fill_parent"
9. android:layout_height="96.5dp"
10. android:background="@drawable/banner" />
11. <TableLayout
12. android:id="@+id/tableLayout1"
13. android:layout_width="match_parent"
14. android:layout_height="wrap_content"
15. android:layout_marginTop="15dp" >
16. <TableRow
17. android:id="@+id/tableRow1"
18. android:layout_width="wrap_content"
19. android:layout_height="wrap_content"
20. android:layout_marginTop="25dp" >
21. <TextView
22. android:id="@+id/Student_Name"
23. android:layout_width="0dp"
24. android:layout_height="wrap_content"
25. android:layout_weight="1"
26. android:text="Student Name: "
27. android:textAppearance="?android:attr/textAppearanceMedium" />
28. </TableRow>
29. <TableRow
30. android:id="@+id/tableRow2"
31. android:layout_width="match_parent"
32. android:layout_height="wrap_content"
33. android:layout_marginBottom="25dp" >
34. <TextView
35. android:id="@+id/student_name_txt"
36. android:layout_width="wrap_content"
37. android:layout_height="wrap_content"
38. android:text="TextView" />
39. </TableRow>
40. <TableRow
41. android:id="@+id/tableRow3"
42. android:layout_width="wrap_content"
43. android:layout_height="wrap_content" >
44. <TextView
45. android:id="@+id/Student_ID"
46. android:layout_width="0dp"
47. android:layout_height="wrap_content"
48. android:layout_weight="1"
49. android:text="Student ID: "
50. android:textAppearance="?android:attr/textAppearanceMedium" />
51. </TableRow>
52. <TableRow
53. android:id="@+id/tableRow4"
54. android:layout_width="match_parent"
55. android:layout_height="wrap_content"
56. android:layout_marginBottom="25dp" >
57. <TextView
58. android:id="@+id/student_id_txt"
59. android:layout_width="wrap_content"
60. android:layout_height="wrap_content"
61. android:text="TextView" />
62. </TableRow>
63. <TableRow
64. android:id="@+id/tableRow7"
65. android:layout_width="wrap_content"
66. android:layout_height="wrap_content" >
67. <TextView
68. android:id="@+id/Time"
69. android:layout_width="wrap_content"
70. android:layout_height="wrap_content"
71. android:text="Time: "
72. android:textAppearance="?android:attr/textAppearanceMedium" />
73. </TableRow>
74. </TableLayout>
75.
103.</LinearLayout>
1.package com.test;
2.import java.io.BufferedReader;
3.import java.io.InputStream;
4.import java.io.InputStreamReader;
5.import java.util.ArrayList;
6.import java.text.DateFormat;
7.import java.text.ParseException;
8.import java.text.SimpleDateFormat;
9.import java.util.Date;
10.import org.apache.http.HttpEntity;
11.import org.apache.http.HttpResponse;
12.import org.apache.http.NameValuePair;
13.import org.apache.http.client.HttpClient;
14.import org.apache.http.client.entity.UrlEncodedFormEntity;
15.import org.apache.http.client.methods.HttpPost;
16.import org.apache.http.impl.client.DefaultHttpClient;
17.import org.apache.http.message.BasicNameValuePair;
18.import android.app.Activity;
19.import android.app.AlertDialog;
20.import android.content.DialogInterface;
21.import android.os.Bundle;
22.import android.util.Log;
23.import android.view.Menu;
24.import android.view.MenuItem;
25.import android.view.View.OnClickListener;
26.import android.widget.EditText;
27.import android.widget.LinearLayout;
28.import android.widget.TextView;
29.public class getdata extends Activity {
30. String result = "";
31. ArrayList arrayData = new ArrayList();
32. TextView txt;
33. @Override
34. public void onCreate(Bundle savedInstanceState) {
35. super.onCreate(savedInstanceState);
36. setContentView(R.layout.main);
37. txt = new TextView(getApplicationContext());
38. txt.setText(getServerData(KEY_121));
39. }
40.public static final String KEY_121 = "http://10.111.6.160/getdata/getdata.php";
41.private String getServerData(String returnString) {
42. String studentident = getIntent().getStringExtra("studentident");
43. LinearLayout MainLL = (LinearLayout) findViewById(R.id.myLinearLayout);
44. TextView studid = (TextView) findViewById(R.id.student_id_txt);
45. TextView studname = (TextView) findViewById(R.id.student_name_txt);
46. InputStream is = null;
47. try {
48. ArrayList nameValuePairs = new ArrayList();
49. nameValuePairs.add(new BasicNameValuePair("studentid", studentident));
50. HttpClient httpclient = new DefaultHttpClient();
51. HttpPost httppost = new HttpPost(KEY_121);
52. httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
53. HttpResponse response = httpclient.execute(httppost);
54. HttpEntity entity = response.getEntity();
55. is = entity.getContent();
56. } catch (Exception e) {
57. Log.e("log_tag", "Error in http connection " + e.toString());
58. }
59. // convert response to string
60. try {
61. BufferedReader reader = new BufferedReader(new InputStreamReader(
62. is, "iso-8859-1"), 8);
63. StringBuilder sb = new StringBuilder();
64. String line = null;
65. while ((line = reader.readLine()) != null) {
66. sb.append(line + "\n");
67. // arrayData.add(line);
68. }
69. is.close();
70. result = sb.toString();
71. } catch (Exception e) {
72. Log.e("log_tag", "Error converting result " + e.toString());
73. }
74. int position1 = result.indexOf("{");
75. if (position1 == -1){
76. studid.setText("Please enter a valid id.");
77. studname.setText("Please enter a valid id.");
78. return " ";
79. }
80. int position2 = result.indexOf("}");
81. int find, cfind, findnull;
82. String sID = "", fNAME = "", lNAME = "", course = "", gTime = "", arrayfree = "";
83. DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
84. while (position1 != -1) {
85. arrayData.add(result.substring(position1 + 1, position2));
86. result = result.substring(position2 + 1);
87. position1 = result.indexOf("{");
88. position2 = result.indexOf("}");
89. }
90. for (int i = 0; i < arrayData.size(); ++i) {
91. arrayfree = arrayData.get(i);
92. find = arrayfree.indexOf(":");
93. cfind = arrayfree.indexOf(",");
94. if (i == 0) {
95. sID = arrayfree.substring(find + 2, find + 9);
96. arrayfree = arrayfree.substring(find + 11);
97. find = arrayfree.indexOf(":");
98. arrayfree = arrayfree.substring(find + 12);
99. find = arrayfree.indexOf(":");
100. cfind = arrayfree.indexOf(",");
101. fNAME = arrayfree.substring(find + 2, cfind - 1);
102. arrayfree = arrayfree.substring(cfind + 1);
103. find = arrayfree.indexOf(":");
104. cfind = arrayfree.indexOf(",");
105. lNAME = arrayfree.substring(find + 2, cfind - 1);
106. arrayfree = arrayfree.substring(cfind + 1);
107. find = arrayfree.indexOf(":");
108. cfind = arrayfree.indexOf(",");
109. course = arrayfree.substring(find + 2, cfind - 1);
110. arrayfree = arrayfree.substring(cfind + 1);
111. find = arrayfree.indexOf(":");
112. findnull = arrayfree.indexOf("n");
113. if (find + 1 == findnull) {
114. gTime = "You are still currently logged in.";
115. } else {
116. gTime = arrayfree.substring(find + 2, find + 10);
117. }
118. final TextView rowTextView = new TextView(this);
119. /*Date tdate=new Date();
120. try {
121. tdate = sdf.parse(gTime);
122. //rowTextView.setText(" " + course + ": " + tdate + " \r\n ");
123. } catch (ParseException e) {
124. // TODO Auto-generated catch block
125. e.printStackTrace();
126. }
127. rowTextView.setText(" " + course + ": " + tdate + " \r\n ");*/
128. rowTextView.setText(" " + course + ": " + gTime + " \r\n ");
129. MainLL.addView(rowTextView);
130. studid.setText(" " + sID);
131. studname.setText(" " + fNAME + " " + lNAME);
132. }
133. else if (i > 0) {
134. arrayfree = arrayfree.substring(find + 11);
135. arrayfree = arrayfree.substring(find + 12);
136. cfind = arrayfree.indexOf(",");
137. arrayfree = arrayfree.substring(cfind + 1);
138. cfind = arrayfree.indexOf(",");
139. arrayfree = arrayfree.substring(cfind + 1);
140. find = arrayfree.indexOf(":");
141. cfind = arrayfree.indexOf(",");
142. course = arrayfree.substring(find + 2, cfind - 1);
143. arrayfree = arrayfree.substring(cfind + 1);
144. find = arrayfree.indexOf(":");
145. findnull = arrayfree.indexOf("n");
146. if (find + 1 == findnull) {
147. gTime = "You are still currently logged in.";
148. } else {
149. gTime = arrayfree.substring(find + 2, find + 10);
150. }
151. final TextView rowTextView = new TextView(this);
152. /*Date tdate = new Date();
153. try {
154. tdate = sdf.parse(gTime);
155. //rowTextView.setText(" " + course + ": " + tdate + " \r\n ");
156. } catch (ParseException e) {
157. // TODO Auto-generated catch block
158. e.printStackTrace();
159. }
160. rowTextView.setText(" " + course + ": " + tdate + " \r\n ");*/
161. rowTextView.setText(" " + course + ": " + gTime + " \r\n ");
162. MainLL.addView(rowTextView);
163. }
164. }
165. return " ";
166. // return result;
167.}
168.@Override
169.public boolean onCreateOptionsMenu(Menu menu) {
170. // TODO Auto-generated method stub
171. menu.add(0, 0, 0, R.string.app_about);
172. menu.add(0, 1, 1, R.string.str_exit);
173. return super.onCreateOptionsMenu(menu);
174.}
175.@Override
176.public boolean onOptionsItemSelected(MenuItem item) {
177. // TODO Auto-generated method stub
178. super.onOptionsItemSelected(item);
179. switch(item.getItemId())
180. {
181. case 0:
182. openOptionsDialog();
183. break;
184. case 1:
185. exitOptionsDialog();
186. break;
187. }
188. return true;
189.}
190.private void openOptionsDialog()
191.{
192.new AlertDialog.Builder(this)
193.setTitle(R.string.app_about)
194. .setMessage(R.string.app_about_message)
195. .setPositiveButton(R.string.str_ok,
196.new DialogInterface.OnClickListener()
197.{
198.public void onClick(DialogInterface dialoginterface, int i)
199.{
200.}
201.})
202. .show();
203.}
204.private void exitOptionsDialog()
205.{
206.new AlertDialog.Builder(this)
207. .setTitle(R.string.app_exit)
208. .setMessage(R.string.app_exit_message)
209. .setNegativeButton(R.string.str_no,
210.new DialogInterface.OnClickListener()
211.{
212.public void onClick(DialogInterface dialoginterface, int i)
213.{}
214.})
215. .setPositiveButton(R.string.str_ok,
216.new DialogInterface.OnClickListener()
217.{
218.public void onClick(DialogInterface dialoginterface, int i)
219.{
220. finish();
221.}
222. .})
223. .show();
224.}
225.}
Below is where we use the php server to access the gotcha database.
1.<?php
2.$message = "There is no data please try again afternoon!";
3.mysql_connect("10.111.0.17","gotchaandroid","gotchaandroid");
4.mysql_select_db("gotcha");
5.$studid = $_REQUEST['studentid'];
6.$q=mysql_query("SELECT DISTINCT STUDENTID, CARDID, STUD_FIRSTNAME, STUD_LASTNAME, COURSEID_CLASS, SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(TIMEOUT,TIMEIN)))) AS 'TIME SPENT'
7.FROM STUDENT, CLASS, ENROLLED, ATTENDANCE
8.WHERE STUDENT.STUDENTID = ENROLLED.STUDENTID_ENROLLED
9.AND CLASS.COURSEID_CLASS = ENROLLED.COURSEID_ENROLLED AND
10.ENROLLED.SC_ID = ATTENDANCE.SC_ID_ATTEND
11.AND STUDENT.STUDENTID = $studid AND WEEK(CURDATE(),7) = WEEK(ATTENDANCE.TIMEIN ,7) AND DATEDIFF(CURDATE(),ATTENDANCE.TIMEIN) < 7
12.GROUP BY COURSEID_CLASS");
13.while($e=mysql_fetch_assoc($q))
14. $output[]=$e;
15.print(json_encode($output));
16.mysql_close();
17.?>
Below this I have re-made the game Duck Hunt which is popular by Nintendo in 1984. This game was created as my a final project for my actionscript class. I have written code shown below to make functioning game.
1.// Name: Marcus Matthews
2.// date: 11/18/2010
3./******************************************
4. import statements
5.******************************************/
6./******************************************
7. declare variables and instance names
8.******************************************/
9.var leftStage:Number=0;
10.var rightStage:Number=550;
11.var topStage:Number=0;
12.var bottomStage:Number=300;
13.var score:Number=0;
14.var intShots:Number=0;
15.var mouseGun:Sound=new Sound();
16.var dogBark:Sound=new Sound();
17.dogBark.attachSound("bark");
18.var cockGun:Sound=new Sound();
19.var missileDuck:Sound=new Sound();
20.var startMusic:Sound=new Sound();
21.startMusic.attachSound("start");
22.var quack:Sound=new Sound();
23.quack.attachSound("quack");
24.var hitcount:Number=0;
25.var hitTotal:Number=0;
26._global.count=0;
27._global.intlevel=1;
28./******************************************
29. create objects
30.******************************************/
31./******************************************
32. define functions
33.******************************************/
34.// BeginGame function will run until the play button is pressed
35.function beginGame ():Void{
36. /*startMusic.attachSound("start");
37. startMusic.stop("start");
38. startMusic.start(0,5);*/
39. Mouse.show();
40. hound_mc.stop();
41. handgun_mc._visible=false;
42. hit_mc._visible=false;
43. shots_mc._visible=false;
44. scoreboard_mc._visible=false;
45. duck2_mc._visible=false;
46.}
47.// This function playGame will ran whenever the play button is pressed
48.function playGame():Void{
49. startMusic.stop("start");
50. startMusic.start(0,10);
51. startMusic.setVolume(20);
52. Mouse.hide();
53. hound_mc.play();
54. handgun_mc._visible=true;
55. hit_mc._visible=true;
56. shots_mc._visible=true;
57. scoreboard_mc._visible=true;
58. duck2_mc._visible=true;
59. mouseGun.attachSound("gunShot");
60. cockGun.attachSound("reload");
61. cockGun.start(0,1);
62. attachMovie("handgun","handgun_mc",10,{_x:265, _y:171});
63. attachMovie("roundL","roundL_mc",9,{_x:275, _y:165});
64.}
65.// this function will call the other functions to move the ducks
66.// on the stage.
67.function moveDuck():Void{
68. this._x += this.xSpeed;
69. this._y += this.ySpeed;
70. if(this._x < leftStage){
71. this._x = leftStage;
72. this.xSpeed = -this.xSpeed;
73. };
74. if(this._x > rightStage){
75. this._x = rightStage;
76. this.xSpeed = -this.xSpeed;
77. };
78. if(this._y < topStage){
79. this._y = topStage;
80. this.ySpeed = -this.ySpeed;
81. };
82. if(this._y > bottomStage){
83. this._y = bottomStage;
84. this.ySpeed = -this.ySpeed;
85. };
86.}
87.//Add this function to the hitduck to move the duck on the y axis to make
88.// it fall from the sky.
89.function fallDuck():Void{
90. this._y += this.ySpeed;
91. if(this._y > bottomStage){
92. delete this.onEnterFrame;
93. removeMovieClip(this);
94. attachMovie("dogAwards","dogAwards_mc",6,{_x:314,_y:281});
95. hitcount++;
96. callDuck();
97. };
98.}
99.// this function is the new movement for the duck to escape of the stage.
100.function duckEscape():Void {
101. this._y-=this.ySpeed;
102. this.gotoAndPlay("flyaway");
103. if(this._y < topStage){
104. delete this.onEnterFrame;
105. removeMovieClip(this);
106. callDuck();
107. };
108.}
109.// this function will be called to change the current onEnterframe the
110.// duck already has to another frame.
111.function duckUp():Void{
112. if(blackDuck_mc){
113. delete this.onEnterFrame;
114. blackDuck_mc.xSpeed=0;
115. blackDuck_mc.ySpeed=15;
116. blackDuck_mc.onEnterFrame=duckEscape;
117. };
118. if(blueDuck_mc){
119. delete this.onEnterFrame;
120. blueDuck_mc.xSpeed=0;
121. blueDuck_mc.ySpeed=15;
122. blueDuck_mc.onEnterFrame=duckEscape;
123. };
124. if(redDuck_mc){
125. delete this.onEnterFrame;
126. redDuck_mc.xSpeed=0;
127. redDuck_mc.ySpeed=15;
128. redDuck_mc.onEnterFrame=duckEscape;
129. };
130.}
131.// When the play button is press its going to call the call duck function
132.// also, when the duck is hit it will go back and call this function too.
133.function callDuck():Void{
134. var randomNum:Number = Math.random();
135. var Intnumber:Number = 0;
136. Intumber = randomNum % 2;
137. trace("The mode number is: " + Intumber);
138. count += 1;
139. intShots=0;
140. shots_mc.gotoAndPlay("loadGun");
141. mouseGun.setVolume(100);
142. cockGun.start(0,1);
143. if(randomNum <= 0.25){
144. attachMovie("blackDuck","blackDuck_mc",3,{_x:268, _y:300});
145. quack.start(0,2);
146. quack.setVolume(80);
147. _root.blackDuck_mc.xSpeed=16;
148. _root.blackDuck_mc.ySpeed=25;
149. _root.blackDuck_mc.onEnterFrame = moveDuck;
150. if(Intumber > 0.50){
151. _root.blackDuck_mc.xSpeed=24;
152. _root.blackDuck_mc.ySpeed=17;
153. };
154. trace(" blackduck changed property values");
155. };
156. if(randomNum <= 0.50 && randomNum >0.25){
157. attachMovie("blueDuck","blueDuck_mc",3,{_x:268, _y:300});
158. quack.start(0,2);
159. quack.setVolume(80);
160. _root.blueDuck_mc.xSpeed=14;
161. _root.blueDuck_mc.ySpeed=-15;
162. _root.blueDuck_mc.onEnterFrame = moveDuck;
163. if(Intumber > 0.50){
164. _root.blueDuck_mc.xSpeed=11;
165. _root.blueDuck_mc.ySpeed=19;
166. };
167. trace(" blueduck changed property values");
168. };
169. if (randomNum > 0.50) {
170. attachMovie("redDuck","redDuck_mc",3,{_x:268, _y:300});
171. quack.start(0,2);
172. quack.setVolume(80);
173. _root.redDuck_mc.xSpeed=-14;
174. _root.redDuck_mc.ySpeed=14;
175. _root.redDuck_mc.onEnterFrame = moveDuck;
176. if(Intumber > 0.50){
177. _root.redDuck_mc.xSpeed=18;
178. _root.redDuck_mc.ySpeed=24;
179. };
180. trace(" redduck changed property values");
181. };
182. hitTotal = hitcount;
183. hit_mc.ducks_txt.text = hitTotal;
184. if(count==10){
185. if(hitTotal <= 6){
186. attachMovie("endgame","endgame_mc",9,{_x:275, _y:165});
187. startMusic.stop("start");
188. startMusic.start(0,5);
189. Mouse.show();
190. hound_mc.stop();
191. roundL_mc.stop();
192. handgun_mc._visible=false;
193. delete handgun_mc.onMouseDown;
194. delete mouseGun;
195. delete cockGun;
196. delete missileDuck;
197. };
198. if(hitTotal > 6){
199. count = 0;
200. intlevel++;
201. level_mc.round_txt.text = intlevel.toString();
202. roundL_mc.sign_mc.level_txt.text = intlevel.toString();
203. nextStage();
204. };
205. trace(roundL_mc.sign_mc.level_txt.text = intlevel.toString());
206. };
207. trace(randomNum);
208. trace(hitTotal);
209.}
210.// RemoveDuck function will remove the duck off the stage and continue to
211.// remove whenever its called. This function keep count of hits and scores
212.// and outputs hitscores to the textbox.
213.function removeDuck(){
214. var hitscore:Number=0;
215. this.gotoAndPlay("hitduck");
216. missileDuck.attachSound("timber");
217. missileDuck.start(0,1);
218. missileDuck.setVolume(90);
219. quack.stop("quack");
220. attachMovie("dogAwards","dogAwards_mc",6,{_x:314,_y:281});
221. if(_root.blackDuck_mc){
222. delete this.onEnterFrame;
223. blackDuck_mc.xSpeed=0;
224. blackDuck_mc.ySpeed=15;
225. blackDuck_mc.onEnterFrame = fallDuck;
226. hitscore = 500;
227. };
228. if(_root.blueDuck_mc){
229. delete this.onEnterFrame;
230. blueDuck_mc.xSpeed=0;
231. blueDuck_mc.ySpeed=15;
232. blueDuck_mc.onEnterFrame = fallDuck;
233. hitscore = 1000;
234. };
235. if(_root.redDuck_mc){
236. delete this.onEnterFrame;
237. redDuck_mc.xSpeed=0;
238. redDuck_mc.ySpeed=15;
239. redDuck_mc.onEnterFrame = fallDuck;
240. hitscore = 1500;
241. };
242. score += hitscore;
243. scoreboard_mc.score_txt.text = score;
244. trace(hitscore);
245.}
246.function nextStage(){
247. hound_mc.gotoAndPlay("Doghunt");
248. roundL_mc.gotoAndPlay("RoundNext");
249. hitcount = 0;
250. startMusic.start(0,5);
251.}
252./******************************************
253. handle events
254.******************************************/
255.// when play button is released call function playgame, and level 1 is counted
256.// to 1 then hides the play button
257.play_btn.onRelease=function(){
258. playGame();
259. level_mc.round_txt.text = intlevel.toString();
260. roundL_mc.sign_mc.level_txt.text = intlevel.toString();
261. play_btn._visible = false;
262. instruction_mc._visible = false;
263. if(hound_mc._totalframes){
264. dogBark.start(0,3);
265. callDuck();
266. };
267.};
268.onEnterFrame = function(){
269. _root.blackDuck_mc.onPress = removeDuck;
270. _root.blueDuck_mc.onPress = removeDuck;
271. _root.redDuck_mc.onPress = removeDuck;
272. handgun_mc.onMouseDown=function(){
273. mouseGun.stop("gunShot");
274. cockGun.stop("reload");
275. mouseGun.start(0,1);
276. intShots++;
277. if(intShots==1){
278. shots_mc.gotoAndPlay("shotOne");
279. };
280. if(intShots==2){
281. shots_mc.gotoAndPlay("shotTwo");
282. };
283. if(intShots==3){
284. shots_mc.gotoAndPlay("emptyGun");
285. mouseGun.setVolume(0);
286. };
287. if(intShots==4){
288. duckUp();
289. };
290. trace(intShots);
291. };
292.};
293./******************************************
294. set property values
295.******************************************/
296./******************************************
297. run now
298.******************************************/
299.beginGame();
300.stop();
Below to this point down is all my C++ code I worked on in class. The first program is the die roller which is a console application that asks user to guess a number. The program chooses a number between 1 and 100 using a random library class base on time shown on the left. On the right it shows you all possible answers user has guessed and the number of times user had to guess to get the answer right. I have code shown below the two images.
1. // Die Roller
2. // Demonstrates generating random numbers
3. #include <iostream>
4. #include <cstdlib>
5. #include <ctime>
6. using namespace std;
7. int main()
8. {
9. srand(time(0)); // seed random number generator based on current time
10. int randomNumber = rand(); // generate random number
11. int die = (randomNumber % 100) + 1; // get a number between 1 and 100
12. int count = 0,guess;
13. do
14. {
15. cout << "Guess the number? " << endl;
16. cin >> guess;
17. ++count;
18. if (guess == die)
19. cout << "You won!!!" << die << endl;
20. if (guess < die)
21. cout << "Too low!!!" << endl;
22. if (guess > die)
23. cout << "Too High!!!" << endl;
24. } while (guess != die);
25. cout << "How many times tried? " << count << endl;
26. system("pause");
27. return 0;
28. }
Below this statement is a simple Math equation used in the console application that will functions as a calculator. The application will ask user which choice of math does he or she would like to use and calls the function based on the users choice. The chosen Math function then calculates the two input numbers and prints out the correct answer.
1. #include <iostream>
2. using namespace std;
3. int calc(int choice1, float a, float b);
4. int main()
5. {
6. int choice1, num1, num2, v;
7. cout << "Enter your choice of function: " << endl;
8. cout << "1.Add" << endl;
9. cout << "2.Subtract" << endl;
10. cout << "3.Multiply" << endl;
11. cout << "4.Divide" << endl;
12. cout << "\n";
13. cin >> choice1;
14. cout <<"Enter a number: " << endl;
15. cin >> num1;
16. cout <<"Enter another number: "<< endl;
17. cin >> num2;
18. v = calc(choice1, num1, num2);
19. cout << "The answer is: " << v << endl;
20. system("pause");
21. return 0;
22. }
23. int calc(int choice1, float a, int b)
24. {
25. if (choice1==1)
26. return a + b;
27. else if (choice1==2)
28. return a - b;
29. else if (choice1==3)
30. return a * b;
31. else if (choice1==4)
32. return a / b;
33. else
34. return 0;
35. }
Here below we have a game of Russian Roulette. when the game loads it randomly selects a number 1-6 You would enter in a number between 1 and 6 then press enter to shoot the gun. Whatever number you enter beware of the outcome you will get to end the game.
1. // Russian Roulette
2. // Demonstrates generating random numbers
3. #include <iostream>
4. #include <cstdlib>
5. #include <ctime>
6. using namespace std;
7. int main()
8. {
9. srand(time(0));
10. int randomNumber = rand();
11. int trigger = (randomNumber % 6) + 1;
12. int count = 0,tries;
13. cout << "This is the game of Russian Roulette" << endl;
14. do
15. {
16. cout << "Enter a number and hit enter on the keyboard to pull the trigger" << endl;
17. cout << "Pull the trigger " << endl;
18. cin >> tries;
19. ++count;
20. if (tries == trigger)
21. cout << "Game Over" << trigger << endl;
22. if (tries < trigger)
23. cout << "Not even close" << endl;
24. if (tries > trigger)
25. cout << "The gun only has 6 rounds" << endl;
26. } while (tries != trigger);
27. cout << "How many times the trigger been pulled: " << count << endl;
28. system("pause");
29. return 0;
30. }
The last C++ code displayed is array that stores names or elements in an alphabetically order from least to greatest until the user enters in the word "quit" in the array which stops the program.
1. #include <iostream>
2. #include <string>
3. #include <vector>
4. using namespace std;
5. int main()
6. {
7. string element;
8. vector<string> crap;
9. vector<string>::iterator myIterator1;
10. vector<string>::iterator myIterator2;
11. vector<string>::const_iterator iter;
12. do
13. {
14. cout << "Enter element for array: ";
15. cin >> element;
16. if(element != "quit")
17. crap.push_back(element);
18. }while(element != "quit");
19. for(myIterator1 = crap.begin(); myIterator1 < crap.end(); ++myIterator1)
20. {
21. for(myIterator2 = myIterator1+1; myIterator2 < crap.end(); ++myIterator2)
22. {
23. if (*myIterator1 > *myIterator2)
24. {
25. element = *myIterator1;
26. *myIterator1 = *myIterator2;
27. *myIterator2 = element;
28. }
29. }
30. }
31. for(iter = crap.begin(); iter != crap.end(); ++iter)
32. cout << *iter << endl;
33. system("pause");
34. return 0;
35. }
Below I made a 2D animation Dr. Seuss story for a final project grade for my animation class using Adobe Flash.
Below is another final project I made for my Photoshop class to receive a certificate in web design.