Trying to get my last assignment in for the quarter, balance my job, and my other class. I would love an extra set of eyes to tell me where in the world my statements go out outside of my function:
This is an implementation file. The associated header is throwing no errors.
I get the following errors:
1.) In the file included from tests.cpp:7:0:
GBoard.cpp:31:2: error: expected unqualified-id before ‘for’
for (int r=0; r<15; r++)
2.) GBoard.cpp:31:14: error: ‘r’ does not name a type
for (int r=0; r<15; r++)
But I am pretty sure 2 is part of my code being outside of the function somehow.
Here is my code, parts redacted so I don't get hit w/ plagiarism:
bool Gfunction::makeMove(int redacted,int redacted,char secret)
{
if(redacted >= 0 && redacted < 15 && redacted >= 0 && redacted<15)
{
if(redacted() == UNFINISHED && function[redacted][redacted] == '.')
function[redacted][redacted] = secret;
return true;
}
else
{
return false;
}
int track = 0;
for(int r=0;r<15;r++)
{
track = 0;
for(int c=0;c<15;c++)
{
if(function[r][c] == secret)
{
track++;
if(track==5)
{
if(secret == 'x')
secret squirrel stuff = X_WON;
else
secret squirrel stuff = O_WON;
return true;
}
}
else
{
track = 0;
}
}
}
for(int r=0;r<15;r++)
{
track = 0;
for(int c=0;c<15;c++)
{
if(function[r][c] == secret)
{
track++;
if(track==5)
{
if(secret == 'x')
secret squirrel stuff = X_WON;
else
secret squirrel stuff = O_WON;
return true;
}
}
else
{
track = 0;
}
}
}
int r = 0, c = 0;
for(int redacted = 0; redacted<15; redacted++)
{
r = redacted;
c = 0;
track = 0;
while(r < 15 && c < 15)
{
if(function[r][c] == secret)
{
track++;
if(track == 5)
{
if(secret == 'x')
secret squirrel stuff = X_WON;
else
secret squirrel stuff = O_WON;
return true;
}
}
else
{
track = 0;
}
r++;
c++;
}
}
for(int redacted = 0; redacted<15; redacted++)
{
r=0;
c=redacted;
track=0;
while(r<15 && c<15)
{
if(function[r][c] == secret)
{
track++;
if(track == 5)
{
if(secret == 'x')
secret squirrel stuff = X_WON;
else
secret squirrel stuff = O_WON;
return true;
}
}
else
{
track = 0;
}
r++;
c++;
}
}
for(int redacted=0; redacted<15; redacted++)
{
r=redacted;
c=15-1;
track=0;
while(r<15 && c>=0)
{
if(function[r][c] == secret)
{
track++;
if(track == 5)
{
if(secret == 'x')
secret squirrel stuff = X_WON;
else
secret squirrel stuff = O_WON;
return true;
}
}
else
{
track = 0;
}
r++;
c--;
}
}
for(int redacted=15-1;redacted>=0;redacted--)
{
r=0;
c=redacted;
track=0;
while(r<15 && c>= 0)
{
if(function[r][c] == secret)
{
track++;
if(track == 5)
{
if(secret == 'x')
secret squirrel stuff = X_WON;
else
secret squirrel stuff = O_WON;
return true;
}
}
else
{
track = 0;
}
r++;
c--;
}
}
for(int r=0;r<15;r++)
{
for(int c=0;c<15;c++)
{
if(function[r][c] == '.')
{
secret squirrel stuff = UNFINISHED;
return true;
}
}
}
secret squirrel stuff = DRAW;
return true;
}
Double check your innermost if statements. For example:
if(track == 5)
{
if(secret == 'x')
secret squirrel stuff = X_WON;
else
secret squirrel stuff = O_WON;
return true;
}
could be
if(track == 5)
{
if(secret == 'x')
secret squirrel stuff = X_WON;
else
secret squirrel stuff = O_WON;
return true;
}
or
if(track == 5)
{
if(secret == 'x')
secret squirrel stuff = X_WON;
else{
secret squirrel stuff = O_WON;
return true;
}
}
depending on you algorithm logic.
Related
I have this code written in Dart that implements the minmax algorithm, With a tic-tac-toe, the computer is playing as "O" and trying to maximize the score, but I get some wrong decision like this, it says (look at the top, the first is the move coordinate and the second is the score) that I have to move in (0,2). But this is not the right decision.
This is my code:
class Ai {
play(state) {
return max_Value(state);
}
// the action parameter holds the move that got as to this state
List max_Value(List state, {action}) {
List v = [null, double.negativeInfinity];
if (terminal(state)) {
return [action, utility(state)];
}
for (var action in actions(state)) {
if (v[1] == 1) return v;
v = max(v, min_Value(result(state, action), action: action));
}
return v;
}
List min_Value(List state, {action}) {
List v = [null, double.infinity];
if (terminal(state)) {
return [action, utility(state)];
}
for (var action in actions(state)) {
if (v[1] == -1) return v;
v = min(v, max_Value(result(state, action), action: action));
}
return v;
}
List min(List prv, List curr) {
return prv[1] < curr[1] ? prv : curr;
}
List max(List prv, List curr) {
return prv[1] > curr[1] ? prv : curr;
}
bool terminal(List state) {
if (state[0][0] == state[0][1] &&
state[0][0] == state[0][2] &&
state[0][0] != "") {
return true;
}
if (state[1][0] == state[1][1] &&
state[1][0] == state[1][2] &&
state[1][0] != "") {
return true;
}
if (state[2][0] == state[2][1] &&
state[2][0] == state[2][2] &&
state[2][0] != "") {
return true;
}
// vertical
if (state[0][0] == state[1][0] &&
state[0][0] == state[2][0] &&
state[0][0] != "") {
return true;
}
if (state[0][1] == state[1][1] &&
state[0][1] == state[2][1] &&
state[0][1] != "") {
return true;
}
if (state[0][2] == state[1][2] &&
state[0][2] == state[2][2] &&
state[0][2] != "") {
return true;
}
// diagonal
if (state[0][0] == state[1][1] &&
state[2][2] == state[0][0] &&
state[0][0] != "") {
return true;
}
if (state[2][0] == state[1][1] &&
state[2][0] == state[0][2] &&
state[2][0] != "") {
return true;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (state[i][j] == "") {
return false;
}
}
}
return true;
}
double utility(List state) {
String winner = "";
// horizontal
if (state[0][0] == state[0][1] && state[0][0] == state[0][2]) {
winner = state[0][0];
}
if (state[1][0] == state[1][1] && state[1][0] == state[1][2]) {
winner = state[1][0];
}
if (state[2][0] == state[2][1] && state[2][0] == state[2][2]) {
winner = state[2][0];
}
// vertical
if (state[0][0] == state[1][0] && state[0][0] == state[2][0]) {
winner = state[0][0];
}
if (state[0][1] == state[1][1] && state[0][1] == state[2][1]) {
winner = state[0][1];
}
if (state[0][2] == state[1][2] && state[0][2] == state[2][2]) {
winner = state[0][2];
}
// diagonal
if (state[0][0] == state[1][1] && state[2][2] == state[0][0]) {
winner = state[0][0];
}
if (state[2][0] == state[1][1] && state[2][0] == state[0][2]) {
winner = state[2][0];
}
if (winner == "O") {
return 1;
} else if (winner == "X") {
return -1;
} else {
return 0;
}
}
List actions(List state) {
List acs = [];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (state[i][j] == "") {
acs.add([i, j]);
}
}
}
return acs;
}
List result(List state, List action) {
List newState = [
["", "", ""],
["", "", ""],
["", "", ""]
];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
newState[i][j] = state[i][j];
}
}
newState[action[0]][action[1]] = player(state);
return newState;
}
player(List state) {
int xnum = 0;
int onum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (state[i][j] == "X") {
xnum++;
} else if (state[i][j] == "O") {
onum++;
}
}
}
if (xnum > onum) {
return "O";
} else {
return "X";
}
}
}
Some mistakes:
This needs to depend on the player to move whether you return max or min:
play(state) {
return max_Value(state);
}
Here (and same for min) v might become an action done later and not the actual action that was performed and lead to the better value.
v = max(v, min_Value(result(state, action), action: action));
Possible improvements:
With a little modification terminal could return the evaluation too and then you don't need the utility function.
You constantly evaluate player with the function. You should have to do that at most once (see first mistake). min and max know whether they are playing X or O. Everything could be simplified a lot and the number of functions reduced without increasing their complexity.
My code failed on haystack = "hello", needle = "ll".
Expected: 2, my output: -1.
Here's my code:
class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) {
return 0;
}
String result = "";
int i = 0;
int j = 0;
while (i < haystack.length() && j < needle.length()) {
if (haystack.charAt(i) == needle.charAt(j)) {
result += needle.charAt(j);
i++;
j++;
}
result = "";
i++;
}
if (result.equals(needle)) {
return i - needle.length();
}
return -1;
}
}
What did I miss?
Thank you!
class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) {
return 0;
}
String result = "";
int i = 0;
int j = 0;
while (i < haystack.length() && j < needle.length()) {
if (haystack.charAt(i) == needle.charAt(j)) {
result += needle.charAt(j);
i++;
j++;
} else {
result = "";
i++;
}
}
if (result.equals(needle)) {
return i - needle.length();
}
return -1;
}
I think this is what you expect.
in your answer, when haystack.charAt(i) == needle.charAt(j) is okay, you add the needle.charAt(j) to result, and you set result empty string. That's why you are wrong.
so, in the end, the result is empty, and return -1;
How to validate Iranian 10 digits national code in Flutter (Dart)?
It has a specific pattern.
Following method is written in Dart based on this android code and is tested to validate Iranian National Code (Melli-Code) in Flutter:
bool validateNationalCode(String nc) {
if (nc.trim() == '') {
return false;
} else if (nc.length != 10) {
return false;
} else {
int sum = 0;
for (int i = 0; i < 9; i++) {
sum += int.parse(nc[i]) * (10 - i);
}
int lastDigit;
int divideRemaining = sum % 11;
if (divideRemaining < 2) {
lastDigit = divideRemaining;
} else {
lastDigit = 11 - (divideRemaining);
}
if (int.parse(nc[9]) == lastDigit) {
return true;
} else {
return false;
}
}
}
In VS Express 2013, I run the following program against certain test cases written in a spec file. The code and a portion of the spec file is as follows:
struct DOB {
int date;
int month;
int year;
};
int stringToValue(char* temp) {
int num = 0;
while (*temp != '-' && *temp != '\0') {
if (((*temp) >= '0') && ((*temp) <= '9')) {
num = num * 10 + ((*temp) - '0');
++temp;
}
}
return num;
}
int isValidFormat(char* dob) {
int length = 0;
for (int i = 0; dob[i] != '\0'; ++i) {
if (!((dob[i] >= '0' && dob[i] <= '9' || dob[i] == '-')))
return 0;
++length;
}
return length;
}
int function(int value1, int value2) {
if (value1>value2) {
return 2;
}
else if (value1<value2) {
return 1;
}
else
return 0;
}
int isLeap(int year) {
if ((year % 4 == 0) && !(year % 100)) {
return 1;
}
else {
if (year % 400 == 0)
return 1;
else
return 0;
}
}
int isValid(struct DOB d) {
if (d.year>0) {
if (d.month>0 && d.month <= 12) {
if (d.month == 2 && isLeap(d.year) == 1) {
if (d.date>0 && d.date <= 29) {
return 1;
}
else if (d.date > 0 && d.date <= 28) {
return 1;
}
else
return 0;
}
else if (d.date == 1 || d.date == 3 || d.date == 5 || d.date == 7 || d.date == 8 || d.date == 10 || d.date == 12){
if (d.date > 0 && d.date <= 31) {
return 1;
}
else
return 0;
}
else {
if (d.date > 0 && d.date <= 30) {
return 1;
}
else
return 0;
}
}
else {
return 0;
}
}
else {
return 0;
}
}
int isOlder(char* dob1, char* dob2) {
struct DOB d1, d2;
if (isValidFormat(dob1)!= 10 && isValidFormat(dob2) != 10)
return -1;
d1.date = stringToValue(dob1);
d2.date = stringToValue(dob2);
d1.month = stringToValue(dob1 + 3);
d2.month = stringToValue(dob2 + 3);
d1.year = stringToValue(dob1 + 6);
d2.year = stringToValue(dob2 + 6);
if (isValid(d1) == 1 && isValid(d2) == 1) {
if (function(d1.year, d2.year) != 0)
return function(d1.year, d2.year);
else if (function(d1.month, d2.month) != 0)
return function(d1.month, d2.month);
else
return function(d1.date, d2.date);
}
else {
return -1;
}
}
In the spec file containing test cases:
TEST_METHOD(isOlderinvalid)
{
Assert::AreEqual(-1, isOlder("1000", "15-07-2000"), L"isOlder: invalid input case failed", LINE_INFO());
Assert::AreEqual(-1, isOlder("15-07-2000", "000"), L"isOlder: invalid input case failed", LINE_INFO());
Assert::AreEqual(-1, isOlder("15-0A-2000", "15-07-2000"), L"isOlder: invalid input case failed", LINE_INFO());
Assert::AreEqual(-1, isOlder("15-13-2000", "15-07-2000"), L"isOlder: invalid input case failed", LINE_INFO());
Assert::AreEqual(-1, isOlder("29-02-2001", "15-07-2000"), L"isOlder: invalid input case failed", LINE_INFO());
}
When I run the following test cases on my local compiler, I get the expected output. But when I run the tests on the above code, I neither pass nor fail the above test cases. I'm honestly not sure what's wrong. It seems like a VS-specific problem rather than a problem with my code.
Wrong error condition
// if (isValidFormat(dob1)!= 10 && isValidFormat(dob2) != 10)
if (isValidFormat(dob1)!= 10 || isValidFormat(dob2) != 10)
return -1;
When only 1 format was invalid, code called weak function stringToValue() which has an infinite loop with invalid input.
int stringToValue(char* temp) {
int num = 0;
while (*temp != '-' && *temp != '\0') {
if (((*temp) >= '0') && ((*temp) <= '9')) {
num = num * 10 + ((*temp) - '0');
++temp; // Move this to outside `if()` test
}
}
return num;
}
Perhaps additional problems.
Unclear how code "worked" on OP's local compiler. Maybe Assert() was not active.
Is there some standard email validator code sample for Java ME or BlackBerry?
public static boolean validateEmailID(String email) {
email = email.trim();
String reverse = new StringBuffer(email).reverse().toString();
if (email == null || email.length() == 0 || email.indexOf("#") == -1) {
return false;
}
int emailLength = email.length();
int atPosition = email.indexOf("#");
int atDot = reverse.indexOf(".");
String beforeAt = email.substring(0, atPosition);
String afterAt = email.substring(atPosition + 1, emailLength);
if (beforeAt.length() == 0 || afterAt.length() == 0) {
return false;
}
for (int i = 0; email.length() - 1 > i; i++) {
char i1 = email.charAt(i);
char i2 = email.charAt(i + 1);
if (i1 == '.' && i2 == '.') {
return false;
}
}
if (email.charAt(atPosition - 1) == '.' || email.charAt(0) == '.' || email.charAt(atPosition + 1) == '.' || afterAt.indexOf("#") != -1 || atDot < 2) {
return false;
}
return true;
}
Use this code for checking the given email id is valid or not,
private static boolean validateEmailID(String email) {
if (email == null || email.length() == 0 || email.indexOf("#") == -1 || email.indexOf(" ") != -1) {
return false;
}
int emailLenght = email.length();
int atPosition = email.indexOf("#");
String beforeAt = email.substring(0, atPosition);
String afterAt = email.substring(atPosition + 1, emailLenght);
if (beforeAt.length() == 0 || afterAt.length() == 0) {
return false;
}
if (email.charAt(atPosition - 1) == '.') {
return false;
}
if (email.charAt(atPosition + 1) == '.') {
return false;
}
if (afterAt.indexOf(".") == -1) {
return false;
}
char dotCh = 0;
for (int i = 0; i < afterAt.length(); i++) {
char ch = afterAt.charAt(i);
if ((ch == 0x2e) && (ch == dotCh)) {
return false;
}
dotCh = ch;
}
if (afterAt.indexOf("#") != -1) {
return false;
}
int ind = 0;
do {
int newInd = afterAt.indexOf(".", ind + 1);
if (newInd == ind || newInd == -1) {
String prefix = afterAt.substring(ind + 1);
if (prefix.length() > 1 && prefix.length() < 6) {
break;
} else {
return false;
}
} else {
ind = newInd;
}
} while (true);
dotCh = 0;
for (int i = 0; i < beforeAt.length(); i++) {
char ch = beforeAt.charAt(i);
if (!((ch >= 0x30 && ch <= 0x39) || (ch >= 0x41 && ch <= 0x5a) || (ch >= 0x61 && ch <= 0x7a)
|| (ch == 0x2e) || (ch == 0x2d) || (ch == 0x5f))) {
return false;
}
if ((ch == 0x2e) && (ch == dotCh)) {
return false;
}
dotCh = ch;
}
return true;
}
You can simply google around for email validation regex pattern. Thats the easiest and efficient way to check email string format. see the link below.
http://www.zparacha.com/ultimate-java-regular-expression-to-validate-email-address/
http://leshazlewood.com/2006/02/04/java-email-address-validation-the-right-way-regular-expression/
You will find many other examples for regex. For Regex, check the following link.
http://www.regular-expressions.info/java.html