CodeSOD: Confession: rect.Contains(point)
by Remy Porter from The Daily WTF on (#T89Q)
A few years ago, Josip M decided to try his hand at building games using C# and the XNA framework. He started implementing the classic board game, Nine Men's Morris, but like most side projects, he lost interest and mostly forgot about it"
"until a few months ago, when he started cleaning up his hard drive, and found the code again. Then he remembered why he lost interest and abandoned the project:
if (Mouse.GetState().LeftButton == ButtonState.Pressed && PositionPhase == true) { #region Positions // First line if (Mouse.GetState().X > ((int)(175 * GraphicScale)) && Mouse.GetState().X < ((int)(325 * GraphicScale)) && Mouse.GetState().Y > ((int)(175 * GraphicScale)) && Mouse.GetState().Y < ((int)(325 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[0, 0] == '\0') { FigurePlacement[0, 0] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(175 * GraphicScale)) && Mouse.GetState().X < ((int)(325 * GraphicScale)) && Mouse.GetState().Y > ((int)(175 * GraphicScale)) && Mouse.GetState().Y < ((int)(325 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[0, 0] == '\0') { FigurePlacement[0, 0] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(175 * GraphicScale)) && Mouse.GetState().Y < ((int)(325 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[3, 0] == '\0') { FigurePlacement[3, 0] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(175 * GraphicScale)) && Mouse.GetState().Y < ((int)(325 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[3, 0] == '\0') { FigurePlacement[3, 0] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(1675 * GraphicScale)) && Mouse.GetState().X < ((int)(1825 * GraphicScale)) && Mouse.GetState().Y > ((int)(175 * GraphicScale)) && Mouse.GetState().Y < ((int)(325 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[6, 0] == '\0') { FigurePlacement[6, 0] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(1675 * GraphicScale)) && Mouse.GetState().X < ((int)(1825 * GraphicScale)) && Mouse.GetState().Y > ((int)(175 * GraphicScale)) && Mouse.GetState().Y < ((int)(325 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[6, 0] == '\0') { FigurePlacement[6, 0] = '2'; ++FigureCount2; PlayerTurn = 1; } // Second line else if (Mouse.GetState().X > ((int)(375 * GraphicScale)) && Mouse.GetState().X < ((int)(525 * GraphicScale)) && Mouse.GetState().Y > ((int)(375 * GraphicScale)) && Mouse.GetState().Y < ((int)(525 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[1, 1] == '\0') { FigurePlacement[1, 1] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(375 * GraphicScale)) && Mouse.GetState().X < ((int)(525 * GraphicScale)) && Mouse.GetState().Y > ((int)(375 * GraphicScale)) && Mouse.GetState().Y < ((int)(525 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[1, 1] == '\0') { FigurePlacement[1, 1] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(375 * GraphicScale)) && Mouse.GetState().Y < ((int)(525 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[3, 1] == '\0') { FigurePlacement[3, 1] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(375 * GraphicScale)) && Mouse.GetState().Y < ((int)(525 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[3, 1] == '\0') { FigurePlacement[3, 1] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(1475 * GraphicScale)) && Mouse.GetState().X < ((int)(1625 * GraphicScale)) && Mouse.GetState().Y > ((int)(375 * GraphicScale)) && Mouse.GetState().Y < ((int)(525 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[5, 1] == '\0') { FigurePlacement[5, 1] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(1475 * GraphicScale)) && Mouse.GetState().X < ((int)(1625 * GraphicScale)) && Mouse.GetState().Y > ((int)(375 * GraphicScale)) && Mouse.GetState().Y < ((int)(525 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[5, 1] == '\0') { FigurePlacement[5, 1] = '2'; ++FigureCount2; PlayerTurn = 1; } // Third line else if (Mouse.GetState().X > ((int)(575 * GraphicScale)) && Mouse.GetState().X < ((int)(725 * GraphicScale)) && Mouse.GetState().Y > ((int)(575 * GraphicScale)) && Mouse.GetState().Y < ((int)(725 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[2, 2] == '\0') { FigurePlacement[2, 2] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(575 * GraphicScale)) && Mouse.GetState().X < ((int)(725 * GraphicScale)) && Mouse.GetState().Y > ((int)(575 * GraphicScale)) && Mouse.GetState().Y < ((int)(725 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[2, 2] == '\0') { FigurePlacement[2, 2] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(575 * GraphicScale)) && Mouse.GetState().Y < ((int)(725 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[3, 2] == '\0') { FigurePlacement[3, 2] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(575 * GraphicScale)) && Mouse.GetState().Y < ((int)(725 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[3, 2] == '\0') { FigurePlacement[3, 2] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(1275 * GraphicScale)) && Mouse.GetState().X < ((int)(1425 * GraphicScale)) && Mouse.GetState().Y > ((int)(575 * GraphicScale)) && Mouse.GetState().Y < ((int)(725 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[4, 2] == '\0') { FigurePlacement[4, 2] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(1275 * GraphicScale)) && Mouse.GetState().X < ((int)(1425 * GraphicScale)) && Mouse.GetState().Y > ((int)(575 * GraphicScale)) && Mouse.GetState().Y < ((int)(725 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[4, 2] == '\0') { FigurePlacement[4, 2] = '2'; ++FigureCount2; PlayerTurn = 1; } // 4th line else if (Mouse.GetState().X > ((int)(175 * GraphicScale)) && Mouse.GetState().X < ((int)(325 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[0, 3] == '\0') { FigurePlacement[0, 3] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(175 * GraphicScale)) && Mouse.GetState().X < ((int)(325 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[0, 3] == '\0') { FigurePlacement[0, 3] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(375 * GraphicScale)) && Mouse.GetState().X < ((int)(525 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[1, 3] == '\0') { FigurePlacement[1, 3] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(375 * GraphicScale)) && Mouse.GetState().X < ((int)(525 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[1, 3] == '\0') { FigurePlacement[1, 3] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(575 * GraphicScale)) && Mouse.GetState().X < ((int)(725 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[2, 3] == '\0') { FigurePlacement[2, 3] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(575 * GraphicScale)) && Mouse.GetState().X < ((int)(725 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[2, 3] == '\0') { FigurePlacement[2, 3] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(1275 * GraphicScale)) && Mouse.GetState().X < ((int)(1425 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[4, 3] == '\0') { FigurePlacement[4, 3] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(1275 * GraphicScale)) && Mouse.GetState().X < ((int)(1425 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[4, 3] == '\0') { FigurePlacement[4, 3] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(1475 * GraphicScale)) && Mouse.GetState().X < ((int)(1625 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[5, 3] == '\0') { FigurePlacement[5, 3] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(1475 * GraphicScale)) && Mouse.GetState().X < ((int)(1625 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[5, 3] == '\0') { FigurePlacement[5, 3] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(1675 * GraphicScale)) && Mouse.GetState().X < ((int)(1825 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[6, 3] == '\0') { FigurePlacement[6, 3] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(1675 * GraphicScale)) && Mouse.GetState().X < ((int)(1825 * GraphicScale)) && Mouse.GetState().Y > ((int)(925 * GraphicScale)) && Mouse.GetState().Y < ((int)(1075 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[6, 3] == '\0') { FigurePlacement[6, 3] = '2'; ++FigureCount2; PlayerTurn = 1; } // 5th line else if (Mouse.GetState().X > ((int)(575 * GraphicScale)) && Mouse.GetState().X < ((int)(725 * GraphicScale)) && Mouse.GetState().Y > ((int)(1275 * GraphicScale)) && Mouse.GetState().Y < ((int)(1425 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[2, 4] == '\0') { FigurePlacement[2, 4] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(575 * GraphicScale)) && Mouse.GetState().X < ((int)(725 * GraphicScale)) && Mouse.GetState().Y > ((int)(1275 * GraphicScale)) && Mouse.GetState().Y < ((int)(1425 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[2, 4] == '\0') { FigurePlacement[2, 4] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(1275 * GraphicScale)) && Mouse.GetState().Y < ((int)(1425 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[3, 4] == '\0') { FigurePlacement[3, 4] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(1275 * GraphicScale)) && Mouse.GetState().Y < ((int)(1425 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[3, 4] == '\0') { FigurePlacement[3, 4] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(1275 * GraphicScale)) && Mouse.GetState().X < ((int)(1425 * GraphicScale)) && Mouse.GetState().Y > ((int)(1275 * GraphicScale)) && Mouse.GetState().Y < ((int)(1425 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[4, 4] == '\0') { FigurePlacement[4, 4] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(1275 * GraphicScale)) && Mouse.GetState().X < ((int)(1425 * GraphicScale)) && Mouse.GetState().Y > ((int)(1275 * GraphicScale)) && Mouse.GetState().Y < ((int)(1425 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[4, 4] == '\0') { FigurePlacement[4, 4] = '2'; ++FigureCount2; PlayerTurn = 1; } // 6th line else if (Mouse.GetState().X > ((int)(375 * GraphicScale)) && Mouse.GetState().X < ((int)(525 * GraphicScale)) && Mouse.GetState().Y > ((int)(1475 * GraphicScale)) && Mouse.GetState().Y < ((int)(1625 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[1, 5] == '\0') { FigurePlacement[1, 5] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(375 * GraphicScale)) && Mouse.GetState().X < ((int)(525 * GraphicScale)) && Mouse.GetState().Y > ((int)(1475 * GraphicScale)) && Mouse.GetState().Y < ((int)(1625 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[1, 5] == '\0') { FigurePlacement[1, 5] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(1475 * GraphicScale)) && Mouse.GetState().Y < ((int)(1625 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[3, 5] == '\0') { FigurePlacement[3, 5] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(1475 * GraphicScale)) && Mouse.GetState().Y < ((int)(1625 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[3, 5] == '\0') { FigurePlacement[3, 5] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(1475 * GraphicScale)) && Mouse.GetState().X < ((int)(1625 * GraphicScale)) && Mouse.GetState().Y > ((int)(1475 * GraphicScale)) && Mouse.GetState().Y < ((int)(1625 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[5, 5] == '\0') { FigurePlacement[5, 5] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(1475 * GraphicScale)) && Mouse.GetState().X < ((int)(1625 * GraphicScale)) && Mouse.GetState().Y > ((int)(1475 * GraphicScale)) && Mouse.GetState().Y < ((int)(1625 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[5, 5] == '\0') { FigurePlacement[5, 5] = '2'; ++FigureCount2; PlayerTurn = 1; } // Last line else if (Mouse.GetState().X > ((int)(175 * GraphicScale)) && Mouse.GetState().X < ((int)(325 * GraphicScale)) && Mouse.GetState().Y > ((int)(1675 * GraphicScale)) && Mouse.GetState().Y < ((int)(1825 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[0, 6] == '\0') { FigurePlacement[0, 6] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(175 * GraphicScale)) && Mouse.GetState().X < ((int)(325 * GraphicScale)) && Mouse.GetState().Y > ((int)(1675 * GraphicScale)) && Mouse.GetState().Y < ((int)(1825 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[0, 6] == '\0') { FigurePlacement[0, 6] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(1675 * GraphicScale)) && Mouse.GetState().Y < ((int)(1825 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[3, 6] == '\0') { FigurePlacement[3, 6] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(925 * GraphicScale)) && Mouse.GetState().X < ((int)(1075 * GraphicScale)) && Mouse.GetState().Y > ((int)(1675 * GraphicScale)) && Mouse.GetState().Y < ((int)(1825 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[3, 6] == '\0') { FigurePlacement[3, 6] = '2'; ++FigureCount2; PlayerTurn = 1; } else if (Mouse.GetState().X > ((int)(1675 * GraphicScale)) && Mouse.GetState().X < ((int)(1825 * GraphicScale)) && Mouse.GetState().Y > ((int)(1675 * GraphicScale)) && Mouse.GetState().Y < ((int)(1825 * GraphicScale)) && PlayerTurn == 1 && FigurePlacement[6, 6] == '\0') { FigurePlacement[6, 6] = '1'; ++FigureCount1; PlayerTurn = 2; } else if (Mouse.GetState().X > ((int)(1675 * GraphicScale)) && Mouse.GetState().X < ((int)(1825 * GraphicScale)) && Mouse.GetState().Y > ((int)(1675 * GraphicScale)) && Mouse.GetState().Y < ((int)(1825 * GraphicScale)) && PlayerTurn == 2 && FigurePlacement[6, 6] == '\0') { FigurePlacement[6, 6] = '2'; ++FigureCount2; PlayerTurn = 1; }
It's worth noting that XNA has a Rectangle structure and a Contains method which would have greatly simplified this code. At least Josip learned from the error of his ways.
[Advertisement] BuildMaster is more than just an automation tool: it brings together the people, process, and practices that allow teams to deliver software rapidly, reliably, and responsibly. And it's incredibly easy to get started; download now and use the built-in tutorials and wizards to get your builds and/or deploys automated!