示例#1
0
			/// <summary>
			/// Sends the start. message with a random int, for determining who the first player is.
			/// </summary>
			public void sendStart(){
				bool pass;
				bool type;
				int number;
				string data;

				Random rand = new Random ();

				MessageStruct newMessage = new MessageStruct ();

				newMessage.Number = rand.Next (1, bluetooth.maxDevices);
				newMessage.Type = true;
				newMessage.Pass = true;
				newMessage.Data = bluetooth.DeviceName;

				byte[] temp = encode(newMessage);
				bluetooth.SendMessages (temp);

				bluetooth.playersNotPlayed = bluetooth.DeviceNames;
			}
示例#2
0
			/// <summary>
			/// Encode the specified pass, type, number and data.
			/// </summary>
			/// <param name="pass">If set to <c>true</c> pass.</param>
			/// <param name="type">If set to <c>true</c> type.</param>
			/// <param name="number">Number.</param>
			/// <param name="data">Data.</param>
			public byte[] encode(MessageStruct str){
				
				string temp = true.ToString() + "*" + str.Pass.ToString () + "*" + str.Type.ToString () + "*" + str.Number + "*" + str.Data;
				return System.Text.Encoding.UTF8.GetBytes (temp);
			}
示例#3
0
			public override void HandleMessage (Message msg)
			{
				switch (msg.What) {
				case MESSAGE_STATE_CHANGE:
					switch (msg.Arg1) {
					case BluetoothChatService.STATE_CONNECTED:
						break;
					case BluetoothChatService.STATE_CONNECTING:
						break;
					case BluetoothChatService.STATE_LISTEN:
					case BluetoothChatService.STATE_NONE:
						//TODO what if someone disconnects
						// which one disconnected?
						// .Remove();
						//bluetoothChat.title.SetText (Resource.String.title_not_connected);
						break;
					}
					break;
				case MESSAGE_WRITE:

					break;
					// reads the message, if it is a device list then updates device list, else updates the message list

					//IF pass is true AND message is False, then we are passing device info
					//IF pass is False, then we are processing a message
					//IF PASS is True AND message is True, then we are starting the game
				case MESSAGE_READ:
					byte[] readBuf = (byte[])msg.Obj;

//					foreach (byte b in readBuf) {
//
//
//					}
					MessageStruct message = new MessageStruct();


					message = decode (readBuf);

					if (message.SetBit) {
						if (message.Pass && !message.Type) {
							//get devices
							// decode byte[] for device names
							if (message.Number != 0) {
								bluetooth.maxDevices = message.Number;
							}
							//ByteArrayToString(
							string[] devices = message.Data.Split (' ');
					
							bool ans = false;

							foreach (string device in devices) {
								// add unique devices to the list
								if (AddDevice (device)) {
									//forward devices
									ans = true;

									Console.WriteLine (device + "\n\t" + bluetooth.devices);
								}

							}
							if (ans) {
								bluetooth.SendMessages (readBuf);
							}

						} else if (!message.Pass) {
							//add message to the messageList
							if (!bluetooth.messages.Contains (message.Data)) {
								bluetooth.messages.Add (message.Data);

								//send the message to all- flooding :)
								bluetooth.SendMessages (readBuf);
								// remove player from list of people who haven't played

									string[] players = Array.ConvertAll<object, string> (bluetooth.playersNotPlayed.ToArray (), x => x.ToString ());
									string player = players [message.Number];
									bluetooth.playersNotPlayed.Remove (player);
									if (player == bluetooth.DeviceName) {
										// TODO MAKE MOVE HERE
										bluetooth.makeMove ();

									} else {
										// NOT YOUR TURN
										// IF YOU HAVE GONE, LOAD DATA



								}
							}

						} else {

							if (!bluetooth.randomDevices.Contains (message.Data)) {
								// calculate if this is the starting device
								// add numbers to the count
								bluetooth.randomCount++;
								Console.WriteLine ("Random Count = " + bluetooth.randomCount);
								bluetooth.randomTotal += message.Number;

								bluetooth.randomDevices.Add (message.Data);

								//forward the message
								bluetooth.SendMessages (readBuf);

								// if all numbers have been received then find average
								if (bluetooth.maxDevices == bluetooth.randomCount) {
									int average = bluetooth.randomCount / bluetooth.maxDevices;
									//TODO SORT DEVICES ? how is this sorting
									bluetooth.DeviceNames.Sort ();
									string[] temp = Array.ConvertAll (bluetooth.DeviceNames.ToArray (), x => x.ToString ());

									// if you match, you are the first player
									if (temp [average].Equals (bluetooth.DeviceName)) {
										// execute turn if it is you TODO
										// START THE GAME HERE ######################## TODO
										// CHOOSE A RANDOM PROMPT

										bluetooth.startGame ();


									} else {
										// you are not the first player
									}
								}
							}
						}
					}
					break;

					// saves the device to the list of devices
				case MESSAGE_DEVICE_NAME:

					MessageStruct tempStruct = new MessageStruct();
					tempStruct.Pass = true;
					tempStruct.Type = false;
					tempStruct.Number = 0;
					tempStruct.Data = "";


					string deviceName = msg.Data.GetString (DEVICE_NAME);
					if(AddDevice(deviceName)){
						bluetooth.directDevices++;

						// put the devices into a string
						foreach (string device in bluetooth.DeviceNames) {

							tempStruct.Data = tempStruct.Data + device + " ";

						}
						if (bluetooth.maxDevices != 0) {
							tempStruct.Number = bluetooth.maxDevices;
						} else {
							tempStruct.Number = 0;
						}

						// sends the devices out to all devices
						byte[] byteMessage = encode(tempStruct);
						bluetooth.SendMessages (byteMessage);

					}
					break;
				case MESSAGE_TOAST:
					Toast.MakeText (Application.Context, msg.Data.GetString (TOAST), ToastLength.Short).Show ();
					break;					
				}
			}
示例#4
0
			/// <summary>
			/// Decode the specified pass, type, number and data.
			/// </summary>
			/// <param name="pass">If set to <c>true</c> pass.</param>
			/// <param name="type">If set to <c>true</c> type.</param>
			/// <param name="number">Number.</param>
			/// <param name="data">Data.</param>
			public MessageStruct decode(byte[] data){


				MessageStruct str = new MessageStruct ();
				string temp = System.Text.Encoding.UTF8.GetString(data);
				string[] bools = temp.Split ('*');
				str.SetBit = System.Convert.ToBoolean (bools [0]);
				str.Pass = System.Convert.ToBoolean(bools [1]);
				str.Type = System.Convert.ToBoolean(bools [2]);
				str.Number = Integer.ParseInt(bools [3]);
				str.Data = bools [4];
				return str;

			}
示例#5
0
		public void makeMove(){

			if (messages.Count % 2 == 1) {

				Android.Graphics.Bitmap image;

				LinearLayout layout = new LinearLayout (this);
				layout.Orientation = Orientation.Vertical;
				layout.SetBackgroundColor(Android.Graphics.Color.White);
				LinearLayout horiLayout = new LinearLayout (this);
				horiLayout.SetBackgroundColor(Android.Graphics.Color.SlateGray);
				horiLayout.Orientation = Orientation.Horizontal;

				Button drawButt = new Button (this);
				drawButt.Text = "Draw";
				horiLayout.AddView(drawButt);

				Button eraseButt = new Button (this);
				eraseButt.Text = "Erase";
				horiLayout.AddView(eraseButt);

				Button clearButt = new Button (this);
				clearButt.Text = "Clear";
				horiLayout.AddView(clearButt);

				Button doneButt = new Button (this);
				doneButt.Text = "Done";
				horiLayout.AddView(doneButt);

				DrawTest dt = new DrawTest(this);

				layout.AddView(horiLayout);
				layout.AddView(dt);

				drawButt.Click += (object sender, EventArgs e) => {

					dt.setColor(false);
				};

				eraseButt.Click += (object sender, EventArgs e) => {

					dt.setColor(true);
				};

				clearButt.Click += (object sender, EventArgs e) => {

					dt.clear();
				};

				doneButt.Click += (object sender, EventArgs e) => {

					image = dt.done();

					string message = "Image Sent.";

					messages.Add(message);

					MessageStruct ms = new MessageStruct();

					ms.Data = message;
					ms.Number = 0;
					ms.Pass = false;
					ms.Type = false;
					ms.SetBit = true;

					SendMessages(handle.encode(ms));

					messagesViewAdapter.Add(message);

					SetContentView(Resource.Layout.GameView);
				};



				SetContentView(layout);


			} else {

				Button doneButt = FindViewById<Button> (Resource.Id.subButton);
				EditText text = FindViewById<EditText> (Resource.Id.messageEntry);

				doneButt.Click+= (object sender, EventArgs e) => {

					MessageStruct str = new MessageStruct();

					str.Data = text.Text;
					str.Type = false;
					str.Pass = false;
					str.SetBit = true;
					str.Number = 0;

					messages.Add(text.Text);
					messagesViewAdapter.Add(text.Text);

					SendMessages(handle.encode(str));
				};
			}


		}