介紹
背景
使用代碼
1)載入類別和單詞:
class WordEntity{ public string Category { get; set; } public string Word { get; set; }}
private string PRESET_WORDS ="COUNTRIES|BANGLADESH|GAMBIA|AUSTRALIA|ENGLAND|NEPAL|INDIA|PAKISTAN|TANZANIA|SRILANKA|CHINA|CANADA|JAPAN|BRAZIL|ARGENTINA|" +"MUSIC|PINKFLOYD|METALLICA|IRONMAIDEN|NOVA|ARTCELL|FEEDBACK|ORTHOHIN|DEFLEPPARD|BEATLES|ADAMS|JACKSON|PARTON|HOUSTON|SHAKIRA|" +...
if (File.Exists(FILE_NAME_FOR_STORING_WORDS)) // If words file exists, then read it. ReadFromFile();else{ // Otherwise create the file and populate from there. string EncryptedWords = StringCipher.Encrypt(PRESET_WORDS, ENCRYPTION_PASSWORD); using (StreamWriter OutputFile = new StreamWriter(FILE_NAME_FOR_STORING_WORDS)) OutputFile.Write(EncryptedWords); ReadFromFile();}
string Str = File.ReadAllText(FILE_NAME_FOR_STORING_WORDS);string[] DecryptedWords = StringCipher.Decrypt(Str, ENCRYPTION_PASSWORD).Split('|');if (DecryptedWords[0].Equals("")) // This means the file was tampered.{ MessageBox.Show("The words file was tampered. Any Categories/Words saved by the player will be lost."); File.Delete(FILE_NAME_FOR_STORING_WORDS); PopulateCategoriesAndWords(); // Circular reference. return;}string Category = "";for (int i = 0; i <= DecryptedWords.GetUpperBound(0); i++){ if (i % (MAX_WORDS + 1) == 0) // Every 15th word is the category name. { Category = DecryptedWords; Categories.Add(Category); } else { WordEntity Word = new WordEntity(); Word.Category = Category; Word.Word = DecryptedWords; WordsList.Add(Word); }}
private void WordsDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e){ e.Control.KeyPress -= new KeyPressEventHandler(Control_KeyPress); e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress);}
TextBox tb = sender as TextBox;if (e.KeyChar == (char)Keys.Enter){ if (tb.Text.Length <= MIN_LENGTH) // Checking length { MessageBox.Show("Words should be at least " + MAX_LENGTH + " characters long."); e.Handled = true; return; }}if (tb.Text.Length >= MAX_LENGTH) // Checking length{ MessageBox.Show("Word length cannot be more than " + MAX_LENGTH + "."); e.Handled = true; return;}if (e.KeyChar.Equals(' ')) // Checking space; no space allowed. Other invalid characters check can be put here instead of the final check on save button click.{ MessageBox.Show("No space, please."); e.Handled = true; return;}e.KeyChar = char.ToUpper(e.KeyChar);
public bool CheckUserInputValidity(DataGridView WordsDataGridView, List<string> WordsByThePlayer){ if (WordsDataGridView.Rows.Count != MAX_WORDS + 1) { MessageBox.Show("You need to have " + MAX_WORDS + " words in the list. Please add more."); return false; } char[] NoLettersList = { ':', ';', '@', '\'', '"', '{', '}', '[', ']', '|', '\\', '<', '>', '?', ',', '.', '/', '`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '~', '!', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+'}; //' foreach (DataGridViewRow Itm in WordsDataGridView.Rows) { if (Itm.Cells[0].Value == null) continue; if (Itm.Cells[0].Value.ToString().IndexOfAny(NoLettersList) >= 0) { MessageBox.Show("Should only contain letters. The word that contains something else other than letters is: '" + Itm.Cells[0].Value.ToString() + "'"); return false; } if (WordsByThePlayer.IndexOf(Itm.Cells[0].Value.ToString()) != -1) { MessageBox.Show("Can't have duplicate word in the list. The duplicate word is: '" + Itm.Cells[0].Value.ToString() + "'"); return false; } WordsByThePlayer.Add(Itm.Cells[0].Value.ToString()); } for (int i = 0; i < WordsByThePlayer.Count - 1; i++) // For every word in the list. { string str = WordsByThePlayer; for (int j = i + 1; j < WordsByThePlayer.Count; j++) // Check existence with every other word starting from the next word if (str.IndexOf(WordsByThePlayer[j]) != -1) { MessageBox.Show("Can't have a word as a sub-part of another word. Such words are: '" + WordsByThePlayer + "' and '" + WordsByThePlayer[j] + "'"); return false; } } return true;}
2)放在網格上:
for (int i = 0, j = PlacementIndex_X; i < Word.Length; i++, j++) // First we check if the word can be placed in the array. For this it needs blanks there.{ if (j >= GridSize) return false; // Falling outside the grid. Hence placement unavailable. if (WORDS_IN_BOARD[j, PlacementIndex_Y] != '\0') if (WORDS_IN_BOARD[j, PlacementIndex_Y] != Word) // If there is an overlap, then we see if the characters match. If matches, then it can still go there. { PlaceAvailable = false; break; }}if (PlaceAvailable){ // If all the cells are blank, or a non-conflicting overlap is available, then this word can be placed there. So place it. for (int i = 0, j = PlacementIndex_X; i < Word.Length; i++, j++) WORDS_IN_BOARD[j, PlacementIndex_Y] = Word; StoreWordPosition(Word, PlacementIndex_X, PlacementIndex_Y, OrientationDecision); return true;}break;
Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));ColourCells(ColouredRectangles, Color.LightBlue);if (FailedRectangles.Count > 0) ColourCells(FailedRectangles, Color.ForestGreen);// Draw horizontal lines.for (int i = 0; i <= GridSize; i++) e.Graphics.DrawLine(pen, 40, (i + 1) * 40, GridSize * 40 + 40, (i + 1) * 40);// Draw vertical lines.for (int i = 0; i <= GridSize; i++) e.Graphics.DrawLine(pen, (i + 1) * 40, 40, (i + 1) * 40, GridSize * 40 + 40);MapArrayToGameBoard();
Graphics formGraphics = CreateGraphics();Font drawFont = new Font("Arial", 16);SolidBrush drawBrush = new SolidBrush(Color.Black);string CharacterToMap;for (int i = 0; i < GridSize; i++) for (int j = 0; j < GridSize; j++) { if (WORDS_IN_BOARD[i, j] != '\0') { CharacterToMap = "" + WORDS_IN_BOARD[i, j]; // "" is needed as a means for conversion of character to string. formGraphics.DrawString(CharacterToMap, drawFont, drawBrush, (i + 1) * 40 + 10, (j + 1) * 40 + 10); } }
if (Points.Count > 1) Points.Pop();if (Points.Count > 0) Points.Push(e.Location);// Form top = X = Distance from top, left = Y = Distance from left.// However mouse location X = Distance from left, Y = Distance from top.// Need an adjustment to exact the location.Point TopLeft = new Point(Top, Left);Point DrawFrom = new Point(TopLeft.Y + Points.ToArray()[0].X + 10, TopLeft.X + Points.ToArray()[0].Y + 80);Point DrawTo = new Point(TopLeft.Y + Points.ToArray()[1].X + 10, TopLeft.X + Points.ToArray()[1].Y + 80);ControlPaint.DrawReversibleLine(DrawFrom, DrawTo, Color.Black); // draw new line
if (Points.Count == 1) return; // This was a doble click, no dragging, hence return.int StartX = Points.ToArray()[1].X / 40; // Retrieve the starting position of the line.int StartY = Points.ToArray()[1].Y / 40;int EndX = Points.ToArray()[0].X / 40; // Retrieve the ending position of the line.int EndY = Points.ToArray()[0].Y / 40;if (StartX > GridSize || EndX > GridSize || StartY > GridSize || EndY > GridSize || // Boundary checks. StartX <= 0 || EndX <= 0 || StartY <= 0 || EndY <= 0){ StatusLabel.Text = "Nope!"; StatusTimer.Start(); return;}StringBuilder TheWordIntended = new StringBuilder();List<Point> TempRectangles = new List<Point>();TheWordIntended.Clear();if (StartY == EndY) // Horizontal line drawn. for (int i = StartX; i <= EndX; i++) { TheWordIntended.Append(WORDS_IN_BOARD[i - 1, StartY - 1].ToString()); TempRectangles.Add(new Point(i * 40, StartY * 40)); }3)計分:
class ScoreEntity{ public string Category { get; set; } public string Scorer { get; set; } public int Score { get; set; } public DateTime ScoreTime { get; set; }............................
4)顯示隱藏的單詞:
List<string> FailedWords = new List<string>();foreach (string Word in WORD_ARRAY) if (WORDS_FOUND.IndexOf(Word) == -1) FailedWords.Add(Word);
foreach (string Word in FailedWords){ WordPosition Pos = WordPositions.Find(p => p.Word.Equals(Word)); if (Pos.Direction == Direction.Horizontal) // Horizontal word. for (int i = Pos.PlacementIndex_X + 1, j = Pos.PlacementIndex_Y + 1, k = 0; k < Pos.Word.Length; i++, k++) FailedRectangles.Add(new Point(i * 40, j * 40)); else if (Pos.Direction == Direction.Vertical) // Vertical word. for (int i = Pos.PlacementIndex_X + 1, j = Pos.PlacementIndex_Y + 1, k = 0; k < Pos.Word.Length; j++, k++) FailedRectangles.Add(new Point(i * 40, j * 40)); else if (Pos.Direction == Direction.DownLeft) // Down left word. for (int i = Pos.PlacementIndex_Y + 1, j = Pos.PlacementIndex_X + 1, k = 0; k < Pos.Word.Length; i--, j++, k++) FailedRectangles.Add(new Point(i * 40, j * 40)); else if (Pos.Direction == Direction.DownRight) // Down right word. for (int i = Pos.PlacementIndex_X + 1, j = Pos.PlacementIndex_Y + 1, k = 0; k < Pos.Word.Length; i++, j++, k++) FailedRectangles.Add(new Point(i * 40, j * 40));}Invalidate();5)作弊碼:
CheatCode += e.KeyCode.ToString().ToUpper();if (CHEAT_CODE.IndexOf(CheatCode) == -1) // Cheat code didn't match with any part of the cheat code. CheatCode = ("" + e.KeyCode).ToUpper(); // Hence erase it to start over.else if (CheatCode.Equals(CHEAT_CODE) && WORDS_FOUND.Count != MAX_WORDS){ Clock.TimeLeft = 86400; // Cheat code applied, literally unlimited time. 86400 seconds equal 1 day. ScoreLabel.Text = "Score: 0"; StatusLabel.Text = "Cheated! Penalty applied!!"; StatusTimer.Start(); CurrentScore = 0; Invalidate();
環境
免責聲明
未來的工作
興趣點
private void GameBoard_MouseMove(object sender, MouseEventArgs e){ try { if (e.Button == MouseButtons.Left) { if (Points.Count > 1) Points.Pop(); if (Points.Count > 0) Points.Push(e.Location); // Form top = X = Distance from top, left = Y = Distance from left. // However mouse location X = Distance from left, Y = Distance from top. // Need an adjustment to exact the location. Point TopLeft = new Point(Top, Left); Point DrawFrom = new Point(TopLeft.Y + Points.ToArray()[0].X + 10, TopLeft.X + Points.ToArray()[0].Y + 80); Point DrawTo = new Point(TopLeft.Y + Points.ToArray()[1].X + 10, TopLeft.X + Points.ToArray()[1].Y + 80); ControlPaint.DrawReversibleLine(DrawFrom, DrawTo, Color.Black); // draw new line } }瑕疵
bug
概要
參考文獻