2015-05-01 14 views
1

我要來試圖從矩陣比較字符比較字符,但它不會增加任何價值,我不知道爲什麼試圖從矩陣C

所以這裏是我的代碼:

#include <stdio.h> 
#include <math.h> 
#include <assert.h> 
#include <limits.h> 
#include <string.h> 
#include <stdlib.h> 

#define MAX_LINES 1000 
#define MAX_LINE_LENGTH 1000 

//--------------------- 
//READING & WRITING 
//--------------------- 

char *ints_new(int n) 
{ 
    return (char *) malloc(n * sizeof(char)); 
} 

char **ints2_new(int rows, int cols) 
{ 
    char **result = (char **) malloc(rows * sizeof(char *)); 
    char *p = ints_new(rows * cols); 
    for (int i = 0; i < rows; i++, p += cols) 
     result[i] = p; 
    return result; 
} 


int str_readline(FILE *f, char *s) 
{ 
    int result = EOF; 
    char *p = fgets(s, INT_MAX, f); 
    if (p != NULL) 
    { 
     result = (int) strlen(s); 
     if (result > 0 && s[result-1] == '\n') 
      s[--result] = '\0'; 
    } 
    return result; 
} 

char *str_dup(const char *s) 
{ 
    char *result = (char *) malloc(strlen(s) + 1); 
    strcpy(result, s); 
    return result; 
} 

int strings_read(FILE *f, char **a) 
{ 
    int result = 0; 
    char line[MAX_LINE_LENGTH + 2]; 
    while (str_readline(f, line) != EOF) 
     a[result++] = str_dup(line); 
    return result; 
} 
// -------------------- 
// Problema A 
// -------------------- 

void values_to_m(char **m, int rows, int cols, char **readings) 
{ 
    int i; 
    int j; 
    int k = 0; 
    int l = 0; 
    for(i = 0; i < rows; i++) 
    { 
     for(j = 0; j < cols; j++) 
     { 
      m[i][j] = readings[k][l]; 
      l++; 
     } 
     k++; 
     l = 0; 
    } 
} 

int count_points(char **m, int i, int j, int rows, int cols) 
{ 
    int result = 0; 
    if(i < rows-2) 
    { 
     if(m[i][j] == m[i+1][j] == m[i+2][j]) 
      result++; 
     if(j < cols-2) 
     { 
      if(m[i][j] == m[i][j+1] == m[i][j+2]) 
       result++; 
      if(m[i][j] == m[i+1][j+1] == m[i+2][j+2]) 
       result++; 
     } 
     if(j > 1) 
     { 
      if(m[i][j] == m[i+1][j-1] == m[i+2][j-2]) 
       result++; 
     } 
    } 
    else 
    { 
     if(j < cols-2) 
     { 
      if(m[i][j] == m[i][j+1] == m[i][j+2]) 
       result++; 
     } 
    } 
    printf("%d\n", result); 
    return result; 
} 

void points(char **m, int rows, int cols) 
{ 
    int i; 
    int j; 
    int player1 = 0; //O's 
    int player2 = 0; //X's 
    for(i = 0; i < rows; i++) 
    { 
     for(j = 0; j < cols; j++) 
     { 
      int count; 
      count = count_points(m, i, j, rows, cols); //counts points 
      if (m[i][j] == 'X') //if values i'm couning are X, points go to player 2 
       player2 += count; 
      else if(m[i][j] == 'O') //if O go to player 1 
       player1 += count; 
     } 
    } 
    printf("%d %d\n", player1, player2); 
} 

// -------------------- 
// -------------------- 

void test_problem_A() 
{ 
    char **readings = malloc((MAX_LINES * MAX_LINE_LENGTH) * sizeof(char) + 1); 
    int rows = strings_read(stdin, readings); //to read from console 
    int cols = strlen(readings[0]); 

    printf("%d\n%d\n", rows, cols); //just to make sure nr of rows and cols is right 
    char **m = ints2_new(rows, cols); //create matrix 

    values_to_m(m, rows, cols, readings); //put the values to matrix 
    points(m, rows, cols); //calculate points 

    ints2_printf(m, rows, cols, "%c"); 
} 

// -------------------- 
// -------------------- 

int main(int argc, char **argv) 
{ 
    test_problem_A(); 
    return 0; 
} 

我程序員必須閱讀一堆'X','O'和'。'。

如果有3「X」中的行(垂直,水平或對角線)播放器2得到1點上,如果發生同樣的情況爲「0」時,播放器1得到1分。 ''不要指望任何點數。

我的矩陣必須有最少3個行列數和最大1000行和cols。

例如: 如果我把控制檯

XXO 
OXO 
OXO 

玩家1和2各得1分

,如果我把:

XXXXXO //(int this line Player 2 get 3 points because there are 3 times 3 X in a row) 
OXOXOO 
OXOOXO 
OXOXOO 

玩家1得到5分 和球員2得到6分

所以我problema爲w ith功能「count_points」它不計算任何點,當我打印「結果」它總是給我0分。

我不能比較2個字符,如果他們在一個矩陣屬於?

感謝

回答

1

count_points,您嘗試以下三個值與表情像

if (a == b == c) ... 

與此相比,沒有做什麼,你認爲它。你把它像在數學符號的比較,但是C將其解釋爲:

if ((a == b) == c) ... 

比較a == b產量爲0或1這導致隨後與c比較。

你可以重寫你想要表達

if (a == b && b == c) ... 

鑑於你abc是複合表達式,你可以寫一個小功能:

static int eq3(int a, int b, int c) 
{ 
    return (a == b && b == c); 
} 

int count_points(char **m, int i, int j, int rows, int cols) 
{ 
    int result = 0; 

    if (i < rows-2) { 
     if (eq3(m[i][j], m[i+1][j], m[i+2][j])) 
      result++; 

     if (j < cols - 2) { 
      if (eq3(m[i][j], m[i][j+1], m[i][j+2])) 
       result++; 
      if (eq3(m[i][j], m[i+1][j+1], m[i+2][j+2])) 
       result++; 
     } 

     if (j > 1) { 
      if (eq3(m[i][j], m[i+1][j-1], m[i+2][j-2])) 
       result++; 
     } 
    } else { 
     if (j < cols-2) { 
      if (eq3(m[i][j], m[i][j+1], m[i][j+2])) 
       result++; 
     } 
    } 

    return result; 
} 

至於分配你的矩陣,請參閱alk的答案。您的分配方法 - 一個char **用於行,然後用於行數據的字符串重複,可能會給您帶來一個不整齊的數組,您可能無法安全地訪問m[j + 1][i],其中i是行j的有效索引,但不適用於行j + 1

+0

我覺得啞巴,這不是第一次這個錯誤發生在我身上。謝謝 –

1

對於初學者來說,在這裏你要指針分配給char

char **readings = malloc((MAX_LINES * MAX_LINE_LENGTH) * sizeof(char) + 1); 

所以這樣做:

char **readings = malloc((MAX_LINES * MAX_LINE_LENGTH) * sizeof(char*) + 1); 

甚至更​​好:

char **readings = malloc((MAX_LINES * MAX_LINE_LENGTH) * sizeof *readings + 1); 
+1

是的,好的。我想他只想要'MAX_LINES',然而,因爲他然後爲每個指針分配一個分配的字符串。 (我不知道+1是爲什麼的;他在最後沒有添加一個'NULL'哨兵。) –

+0

非常感謝,我在互聯網xD上學到了這種方法,我並不熟悉這種方法撥 –